-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_ai.php
More file actions
124 lines (102 loc) · 3.32 KB
/
upload_ai.php
File metadata and controls
124 lines (102 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// upload_ai.php
header("Content-Type: application/json");
// -------------------------------
// CONFIG — EDIT THESE
// -------------------------------
$HUGGINGFACE_TOKEN = "35zqzztS09h1JSavStHwWUgQ3Ug_5bcvys1Yc3Cwj2FaJdjQG"; // Put new token here (PRIVATE)
$MODEL = "facebook/detr-resnet-50"; // Or your custom model
$UPLOAD_DIR = __DIR__ . '/uploads/';
// Create folder if missing
if (!file_exists($UPLOAD_DIR)) {
mkdir($UPLOAD_DIR, 0755, true);
}
// Helper function
function json_error($msg) {
echo json_encode(["success" => false, "message" => $msg]);
exit;
}
// -------------------------------
// 1. Check file upload
// -------------------------------
if (!isset($_FILES['file'])) {
json_error("No file uploaded");
}
$file = $_FILES['file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
json_error("Upload error code: " . $file['error']);
}
// Sanitize filename and save
$basename = preg_replace('/[^A-Za-z0-9_\-\.]/', '_', basename($file['name']));
$target = $UPLOAD_DIR . time() . "_" . $basename;
if (!move_uploaded_file($file['tmp_name'], $target)) {
json_error("Failed to save uploaded file");
}
// Read bytes for HF API
$imageBytes = file_get_contents($target);
// -------------------------------
// 2. CALL HUGGINGFACE INFERENCE API
// -------------------------------
$hfUrl = "https://api-inference.huggingface.co/models/" . $MODEL;
$ch = curl_init($hfUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . $HUGGINGFACE_TOKEN,
"Content-Type: application/octet-stream"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $imageBytes);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// -------------------------------
// 3. Handle HF "model loading" case
// -------------------------------
if ($httpcode == 503) {
json_error("Model loading on HuggingFace. Try again in 5–10 seconds.");
}
// Debug if HF returns error
if ($httpcode !== 200) {
echo json_encode([
"success" => false,
"http_code" => $httpcode,
"hf_response" => $response
]);
exit;
}
// Decode HF output
$pred = json_decode($response, true);
if (!is_array($pred)) {
json_error("Invalid HF response: " . $response);
}
// -------------------------------
// 4. Process predictions
// -------------------------------
$bottleDetected = false;
$threshold = 0.50;
$predictions = [];
foreach ($pred as $item) {
$label = strtolower($item["label"] ?? "");
$score = floatval($item["score"] ?? 0);
$box = $item["box"] ?? null;
// Collect prediction for logging/debug
$predictions[] = [
"label" => $label,
"score" => $score,
"box" => $box
];
// Bottle detected?
if ($label === "bottle" && $score >= $threshold) {
$bottleDetected = true;
}
}
$result = $bottleDetected ? "bottle" : "not_bottle";
// -------------------------------
// 5. Return clean JSON back to ESP32
// -------------------------------
echo json_encode([
"success" => true,
"result" => $result, // "bottle" or "not_bottle"
"predictions" => $predictions // optional debug info
]);
?>