ArchiMathur commited on
Commit
eda777f
·
verified ·
1 Parent(s): 5b83d94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -9,40 +9,61 @@ 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
- def capture_video():
13
- """Attempts to capture video from the default camera (index 0).
14
 
15
- Returns:
16
- cv2.VideoCapture: The video capture object if successful, None otherwise.
17
- """
 
 
 
 
 
18
 
19
- cap = cv2.VideoCapture(0)
20
 
21
- if not cap.isOpened():
22
- print("Error: Could not open video capture device.")
23
- return None
 
 
24
 
25
- return cap
26
 
27
- # Capture video
28
- cap = capture_video()
29
- if cap is None:
30
- # Handle capture failure gracefully
31
- exit()
32
 
33
- while True:
34
- ret, frame = cap.read()
 
 
 
 
 
 
35
 
36
- if not ret:
37
- print("Error: Could not read frame.")
38
- break
 
 
 
 
 
 
 
 
39
 
40
- # Process the frame here (e.g., display, save, analyze)
41
- cv2.imshow('Frame', frame)
 
 
 
42
 
 
43
  if cv2.waitKey(1) & 0xFF == ord('q'):
44
  break
45
 
46
- # Release resources
47
- cap.release()
48
- cv2.destroyAllWindows()
 
9
  # Set the title of the app
10
  st.title("Live Fire Detection App")
11
 
12
+ # Load the video capture
13
+ videoCap = cv2.VideoCapture(0)
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
+ ret, frame = videoCap.read()
27
+ if not ret:
28
+ continue
29
+ results = yolo.track(frame, stream=True)
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
+ # break the loop if 'q' is pressed
64
  if cv2.waitKey(1) & 0xFF == ord('q'):
65
  break
66
 
67
+ # release the video capture and destroy all windows
68
+ videoCap.release()
69
+ cv2.destroyAllWindows()