Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -50,46 +50,73 @@ async def detect(file: UploadFile = File(...)):
|
|
| 50 |
contents = await file.read()
|
| 51 |
filename = file.filename.lower()
|
| 52 |
|
| 53 |
-
#
|
| 54 |
if any(filename.endswith(ext) for ext in ['.mp4', '.avi', '.mov', '.mkv']):
|
| 55 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp:
|
| 56 |
tmp.write(contents)
|
| 57 |
tmp_path = tmp.name
|
| 58 |
|
| 59 |
-
cap
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
cap.release()
|
| 62 |
os.unlink(tmp_path)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
#
|
| 70 |
else:
|
| 71 |
try:
|
| 72 |
img = Image.open(io.BytesIO(contents))
|
| 73 |
except Exception as e:
|
| 74 |
return {"error": str(e), "total": 0, "detections": []}
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 50 |
contents = await file.read()
|
| 51 |
filename = file.filename.lower()
|
| 52 |
|
| 53 |
+
# โโ ููุฏูู โโ
|
| 54 |
if any(filename.endswith(ext) for ext in ['.mp4', '.avi', '.mov', '.mkv']):
|
| 55 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp:
|
| 56 |
tmp.write(contents)
|
| 57 |
tmp_path = tmp.name
|
| 58 |
|
| 59 |
+
cap = cv2.VideoCapture(tmp_path)
|
| 60 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 61 |
+
|
| 62 |
+
# ูุญูู 5 frames ู
ูุฒุนุฉ ุนูู ุงูููุฏูู
|
| 63 |
+
sample_points = [int(total_frames * i / 5) for i in range(1, 6)]
|
| 64 |
+
all_detections = []
|
| 65 |
+
|
| 66 |
+
for frame_num in sample_points:
|
| 67 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
|
| 68 |
+
ret, frame = cap.read()
|
| 69 |
+
if not ret:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 73 |
+
results = model(img)
|
| 74 |
+
|
| 75 |
+
for box in results[0].boxes:
|
| 76 |
+
cls = int(box.cls)
|
| 77 |
+
conf = float(box.conf)
|
| 78 |
+
xywhn = box.xywhn[0].tolist()
|
| 79 |
+
area = xywhn[2] * xywhn[3]
|
| 80 |
+
all_detections.append({
|
| 81 |
+
"damage_type": CLASS_NAMES.get(cls, 'other'),
|
| 82 |
+
"confidence" : round(conf, 3),
|
| 83 |
+
"severity" : get_severity(conf, area),
|
| 84 |
+
"bbox" : xywhn,
|
| 85 |
+
"frame" : frame_num
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
cap.release()
|
| 89 |
os.unlink(tmp_path)
|
| 90 |
|
| 91 |
+
return {
|
| 92 |
+
"total" : len(all_detections),
|
| 93 |
+
"detections": all_detections
|
| 94 |
+
}
|
| 95 |
|
| 96 |
+
# โโ ุตูุฑุฉ โโ
|
| 97 |
else:
|
| 98 |
try:
|
| 99 |
img = Image.open(io.BytesIO(contents))
|
| 100 |
except Exception as e:
|
| 101 |
return {"error": str(e), "total": 0, "detections": []}
|
| 102 |
|
| 103 |
+
results = model(img)
|
| 104 |
+
all_detections = []
|
| 105 |
+
|
| 106 |
+
for box in results[0].boxes:
|
| 107 |
+
cls = int(box.cls)
|
| 108 |
+
conf = float(box.conf)
|
| 109 |
+
xywhn = box.xywhn[0].tolist()
|
| 110 |
+
area = xywhn[2] * xywhn[3]
|
| 111 |
+
all_detections.append({
|
| 112 |
+
"damage_type": CLASS_NAMES.get(cls, 'other'),
|
| 113 |
+
"confidence" : round(conf, 3),
|
| 114 |
+
"severity" : get_severity(conf, area),
|
| 115 |
+
"bbox" : xywhn,
|
| 116 |
+
"frame" : 0
|
| 117 |
+
})
|
| 118 |
+
|
| 119 |
+
return {
|
| 120 |
+
"total" : len(all_detections),
|
| 121 |
+
"detections": all_detections
|
| 122 |
+
}
|