Rahaf2001 commited on
Commit
f9da080
ยท
verified ยท
1 Parent(s): 0059dab

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +55 -28
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 = cv2.VideoCapture(tmp_path)
60
- ret, frame = cap.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  cap.release()
62
  os.unlink(tmp_path)
63
 
64
- if not ret:
65
- return {"error": "cannot read video", "total": 0, "detections": []}
66
-
67
- img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
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
- results = model(img)
77
- detections = []
78
-
79
- for box in results[0].boxes:
80
- cls = int(box.cls)
81
- conf = float(box.conf)
82
- xywhn = box.xywhn[0].tolist()
83
- area = xywhn[2] * xywhn[3]
84
-
85
- detections.append({
86
- "damage_type": CLASS_NAMES.get(cls, 'other'),
87
- "confidence" : round(conf, 3),
88
- "severity" : get_severity(conf, area),
89
- "bbox" : xywhn
90
- })
91
-
92
- return {
93
- "total" : len(detections),
94
- "detections": detections
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
+ }