Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -36,29 +36,33 @@ def process_image(path):
|
|
| 36 |
out = []
|
| 37 |
for r in results:
|
| 38 |
for box in r.boxes:
|
| 39 |
-
cls
|
| 40 |
-
conf
|
| 41 |
xywhn = box.xywhn[0].tolist()
|
| 42 |
lat, lng = random_riyadh()
|
| 43 |
out.append({
|
| 44 |
"damage_type": CLASS_NAMES.get(cls, "other"),
|
| 45 |
-
"confidence": round(conf, 3),
|
| 46 |
-
"severity": severity(conf, xywhn[2] * xywhn[3]),
|
| 47 |
-
"bbox": xywhn,
|
| 48 |
-
"frame": 0,
|
| 49 |
-
"latitude": lat,
|
| 50 |
-
"longitude": lng,
|
| 51 |
})
|
| 52 |
return out
|
| 53 |
|
| 54 |
|
| 55 |
def process_video(path):
|
| 56 |
results = model.track(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
seen = {}
|
| 62 |
|
| 63 |
for frame_idx, r in enumerate(results):
|
| 64 |
if r.boxes is None or r.boxes.id is None:
|
|
@@ -71,15 +75,16 @@ def process_video(path):
|
|
| 71 |
r.boxes.xywhn.tolist(),
|
| 72 |
):
|
| 73 |
if tid not in seen or conf > seen[tid]["confidence"]:
|
| 74 |
-
lat
|
|
|
|
| 75 |
seen[tid] = {
|
| 76 |
"damage_type": CLASS_NAMES.get(cls, "other"),
|
| 77 |
-
"confidence": round(conf, 3),
|
| 78 |
-
"severity": severity(conf, xywhn[2] * xywhn[3]),
|
| 79 |
-
"bbox": xywhn,
|
| 80 |
-
"frame": frame_idx,
|
| 81 |
-
"latitude": lat,
|
| 82 |
-
"longitude": lng,
|
| 83 |
}
|
| 84 |
|
| 85 |
return list(seen.values())
|
|
@@ -93,7 +98,7 @@ def root():
|
|
| 93 |
@app.post("/detect")
|
| 94 |
async def detect(file: UploadFile = File(...)):
|
| 95 |
contents = await file.read()
|
| 96 |
-
suffix
|
| 97 |
|
| 98 |
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
| 99 |
tmp.write(contents)
|
|
@@ -104,4 +109,4 @@ async def detect(file: UploadFile = File(...)):
|
|
| 104 |
finally:
|
| 105 |
os.unlink(tmp_path)
|
| 106 |
|
| 107 |
-
return {"total": len(detections), "detections": detections}
|
|
|
|
| 36 |
out = []
|
| 37 |
for r in results:
|
| 38 |
for box in r.boxes:
|
| 39 |
+
cls = int(box.cls)
|
| 40 |
+
conf = float(box.conf)
|
| 41 |
xywhn = box.xywhn[0].tolist()
|
| 42 |
lat, lng = random_riyadh()
|
| 43 |
out.append({
|
| 44 |
"damage_type": CLASS_NAMES.get(cls, "other"),
|
| 45 |
+
"confidence" : round(conf, 3),
|
| 46 |
+
"severity" : severity(conf, xywhn[2] * xywhn[3]),
|
| 47 |
+
"bbox" : xywhn,
|
| 48 |
+
"frame" : 0,
|
| 49 |
+
"latitude" : lat,
|
| 50 |
+
"longitude" : lng,
|
| 51 |
})
|
| 52 |
return out
|
| 53 |
|
| 54 |
|
| 55 |
def process_video(path):
|
| 56 |
results = model.track(
|
| 57 |
+
source=path,
|
| 58 |
+
conf=0.25,
|
| 59 |
+
tracker="bytetrack.yaml",
|
| 60 |
+
stream=True,
|
| 61 |
+
verbose=False,
|
| 62 |
+
save=False,
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
seen = {}
|
| 66 |
|
| 67 |
for frame_idx, r in enumerate(results):
|
| 68 |
if r.boxes is None or r.boxes.id is None:
|
|
|
|
| 75 |
r.boxes.xywhn.tolist(),
|
| 76 |
):
|
| 77 |
if tid not in seen or conf > seen[tid]["confidence"]:
|
| 78 |
+
lat = seen[tid]["latitude"] if tid in seen else random_riyadh()[0]
|
| 79 |
+
lng = seen[tid]["longitude"] if tid in seen else random_riyadh()[1]
|
| 80 |
seen[tid] = {
|
| 81 |
"damage_type": CLASS_NAMES.get(cls, "other"),
|
| 82 |
+
"confidence" : round(conf, 3),
|
| 83 |
+
"severity" : severity(conf, xywhn[2] * xywhn[3]),
|
| 84 |
+
"bbox" : xywhn,
|
| 85 |
+
"frame" : frame_idx,
|
| 86 |
+
"latitude" : lat,
|
| 87 |
+
"longitude" : lng,
|
| 88 |
}
|
| 89 |
|
| 90 |
return list(seen.values())
|
|
|
|
| 98 |
@app.post("/detect")
|
| 99 |
async def detect(file: UploadFile = File(...)):
|
| 100 |
contents = await file.read()
|
| 101 |
+
suffix = "." + file.filename.split(".")[-1]
|
| 102 |
|
| 103 |
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
| 104 |
tmp.write(contents)
|
|
|
|
| 109 |
finally:
|
| 110 |
os.unlink(tmp_path)
|
| 111 |
|
| 112 |
+
return {"total": len(detections), "detections": detections}
|