ArchiMathur commited on
Commit
f5a6f5b
·
verified ·
1 Parent(s): c1f7bbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -69
app.py CHANGED
@@ -27,7 +27,11 @@ if input_option == "Upload Image":
27
 
28
  # Make predictions
29
  results = model.predict(source=img_np, conf=0.5)
30
-
 
 
 
 
31
  # Draw bounding boxes on the image
32
  for result in results:
33
  boxes = result.boxes.xyxy
@@ -38,100 +42,100 @@ if input_option == "Upload Image":
38
  # Show the resulting image
39
  st.image(img_np, caption='Detected Fire', use_column_width=True)
40
 
41
- elif input_option == "Use Webcam":
42
- st.write("Starting webcam for live detection...")
43
 
44
- # Start video capture
45
- camera = cv2.VideoCapture(0) # 0 is the default camera
46
 
47
- # Create a placeholder for the video feed
48
- video_placeholder = st.empty()
49
 
50
- # Main loop for live detection
51
- while True:
52
- ret, frame = camera.read()
53
- if not ret:
54
- st.write("Failed to capture image")
55
- break
56
 
57
- # Make predictions
58
- results = model.predict(source=frame, conf=0.5)
59
 
60
- # Draw bounding boxes on the frame
61
- for result in results:
62
- boxes = result.boxes.xyxy
63
- for box in boxes:
64
- x1, y1, x2, y2 = box[:4].astype(int)
65
- frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
66
 
67
- # Convert frame to RGB
68
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
69
 
70
- # Display the frame in the Streamlit app
71
- video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
72
 
73
- # Break loop on user command
74
- if st.button("Stop Detection"):
75
- break
76
 
77
- # Release the camera
78
- camera.release()
79
 
80
 
81
 
82
 
83
 
84
- elif input_option == "Upload Video":
85
- uploaded_video = st.file_uploader("Choose a video", type=["mp4", "avi", "mov", "mkv"])
86
- if uploaded_video is not None:
87
- # Save the uploaded video temporarily
88
- temp_video_path = "temp_video.mp4"
89
- with open(temp_video_path, "wb") as f:
90
- f.write(uploaded_video.read())
91
 
92
- # Display the uploaded video
93
- st.video(temp_video_path)
94
 
95
- # Open the video file
96
- video_capture = cv2.VideoCapture(temp_video_path)
97
 
98
- # Create a placeholder for video frame processing
99
- video_frame_placeholder = st.empty()
100
- fire_detected = False
101
 
102
- # Loop through video frames
103
- while video_capture.isOpened():
104
- ret, frame = video_capture.read()
105
- if not ret:
106
- break
107
 
108
- # Make predictions using your fire detection model
109
- results = model.predict(source=frame, conf=0.5)
110
 
111
 
112
 
113
- # Draw bounding boxes on the frame if fire is detected
114
- for result in results:
115
- boxes = result.boxes.xyxy
116
- for box in boxes:
117
- x1, y1, x2, y2 = box[:4].astype(int)
118
- frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
119
- fire_detected = True # Set fire_detected flag if a bounding box is found
120
 
121
- # Convert the frame to RGB format
122
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
123
 
124
- # Display the processed frame
125
- video_frame_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
126
 
127
- # Display detection result
128
- if fire_detected:
129
- st.write("Fire detected in the video.")
130
- else:
131
- st.write("No fire detected in the video.")
132
 
133
- # Release the video capture
134
- video_capture.release()
135
 
136
 
137
 
 
27
 
28
  # Make predictions
29
  results = model.predict(source=img_np, conf=0.5)
30
+
31
+ if Fire_Detected= True:
32
+ print("Fire Detected")
33
+ else:
34
+ print("Not")
35
  # Draw bounding boxes on the image
36
  for result in results:
37
  boxes = result.boxes.xyxy
 
42
  # Show the resulting image
43
  st.image(img_np, caption='Detected Fire', use_column_width=True)
44
 
45
+ # elif input_option == "Use Webcam":
46
+ # st.write("Starting webcam for live detection...")
47
 
48
+ # # Start video capture
49
+ # camera = cv2.VideoCapture(0) # 0 is the default camera
50
 
51
+ # # Create a placeholder for the video feed
52
+ # video_placeholder = st.empty()
53
 
54
+ # # Main loop for live detection
55
+ # while True:
56
+ # ret, frame = camera.read()
57
+ # if not ret:
58
+ # st.write("Failed to capture image")
59
+ # break
60
 
61
+ # # Make predictions
62
+ # results = model.predict(source=frame, conf=0.5)
63
 
64
+ # # Draw bounding boxes on the frame
65
+ # for result in results:
66
+ # boxes = result.boxes.xyxy
67
+ # for box in boxes:
68
+ # x1, y1, x2, y2 = box[:4].astype(int)
69
+ # frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
70
 
71
+ # # Convert frame to RGB
72
+ # rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
73
 
74
+ # # Display the frame in the Streamlit app
75
+ # video_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
76
 
77
+ # # Break loop on user command
78
+ # if st.button("Stop Detection"):
79
+ # break
80
 
81
+ # # Release the camera
82
+ # camera.release()
83
 
84
 
85
 
86
 
87
 
88
+ # elif input_option == "Upload Video":
89
+ # uploaded_video = st.file_uploader("Choose a video", type=["mp4", "avi", "mov", "mkv"])
90
+ # if uploaded_video is not None:
91
+ # # Save the uploaded video temporarily
92
+ # temp_video_path = "temp_video.mp4"
93
+ # with open(temp_video_path, "wb") as f:
94
+ # f.write(uploaded_video.read())
95
 
96
+ # # Display the uploaded video
97
+ # st.video(temp_video_path)
98
 
99
+ # # Open the video file
100
+ # video_capture = cv2.VideoCapture(temp_video_path)
101
 
102
+ # # Create a placeholder for video frame processing
103
+ # video_frame_placeholder = st.empty()
104
+ # fire_detected = False
105
 
106
+ # # Loop through video frames
107
+ # while video_capture.isOpened():
108
+ # ret, frame = video_capture.read()
109
+ # if not ret:
110
+ # break
111
 
112
+ # # Make predictions using your fire detection model
113
+ # results = model.predict(source=frame, conf=0.5)
114
 
115
 
116
 
117
+ # # Draw bounding boxes on the frame if fire is detected
118
+ # for result in results:
119
+ # boxes = result.boxes.xyxy
120
+ # for box in boxes:
121
+ # x1, y1, x2, y2 = box[:4].astype(int)
122
+ # frame = cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
123
+ # fire_detected = True # Set fire_detected flag if a bounding box is found
124
 
125
+ # # Convert the frame to RGB format
126
+ # rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
127
 
128
+ # # Display the processed frame
129
+ # video_frame_placeholder.image(rgb_frame, channels="RGB", use_column_width=True)
130
 
131
+ # # Display detection result
132
+ # if fire_detected:
133
+ # st.write("Fire detected in the video.")
134
+ # else:
135
+ # st.write("No fire detected in the video.")
136
 
137
+ # # Release the video capture
138
+ # video_capture.release()
139
 
140
 
141