Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,61 +9,19 @@ model = YOLO("best.pt") # Ensure the path to your model is correct
|
|
| 9 |
# Set the title of the app
|
| 10 |
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# Function to get class colors
|
| 16 |
-
def getColours(cls_num):
|
| 17 |
-
base_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
|
| 18 |
-
color_index = cls_num % len(base_colors)
|
| 19 |
-
increments = [(1, -2, 1), (-2, 1, -1), (1, -1, 2)]
|
| 20 |
-
color = [base_colors[color_index][i] + increments[color_index][i] *
|
| 21 |
-
(cls_num // len(base_colors)) % 256 for i in range(3)]
|
| 22 |
-
return tuple(color)
|
| 23 |
-
|
| 24 |
|
| 25 |
while True:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
results
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
for result in results:
|
| 33 |
-
# get the classes names
|
| 34 |
-
classes_names = result.names
|
| 35 |
-
|
| 36 |
-
# iterate over each box
|
| 37 |
-
for box in result.boxes:
|
| 38 |
-
# check if confidence is greater than 40 percent
|
| 39 |
-
if box.conf[0] > 0.4:
|
| 40 |
-
# get coordinates
|
| 41 |
-
[x1, y1, x2, y2] = box.xyxy[0]
|
| 42 |
-
# convert to int
|
| 43 |
-
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
| 44 |
-
|
| 45 |
-
# get the class
|
| 46 |
-
cls = int(box.cls[0])
|
| 47 |
-
|
| 48 |
-
# get the class name
|
| 49 |
-
class_name = classes_names[cls]
|
| 50 |
-
|
| 51 |
-
# get the respective colour
|
| 52 |
-
colour = getColours(cls)
|
| 53 |
-
|
| 54 |
-
# draw the rectangle
|
| 55 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), colour, 2)
|
| 56 |
-
|
| 57 |
-
# put the class name and confidence on the image
|
| 58 |
-
cv2.putText(frame, f'{classes_names[int(box.cls[0])]} {box.conf[0]:.2f}', (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, colour, 2)
|
| 59 |
-
|
| 60 |
-
# show the image
|
| 61 |
-
cv2.imshow('frame', frame)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 65 |
break
|
| 66 |
|
| 67 |
-
|
| 68 |
-
videoCap.release()
|
| 69 |
cv2.destroyAllWindows()
|
|
|
|
| 9 |
# Set the title of the app
|
| 10 |
st.title("Live Fire Detection App")
|
| 11 |
|
| 12 |
+
webcamera = cv2.VideoCapture(0)
|
| 13 |
+
# webcamera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
|
| 14 |
+
# webcamera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
while True:
|
| 17 |
+
success, frame = webcamera.read()
|
| 18 |
+
|
| 19 |
+
results = model.track(frame, classes=0, conf=0.8, imgsz=480)
|
| 20 |
+
cv2.putText(frame, f"Total: {len(results[0].boxes)}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
|
| 21 |
+
cv2.imshow("Live Camera", results[0].plot())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
if cv2.waitKey(1) == ord('q'):
|
|
|
|
| 24 |
break
|
| 25 |
|
| 26 |
+
webcamera.release()
|
|
|
|
| 27 |
cv2.destroyAllWindows()
|