Prathamesh1420 commited on
Commit
26663dd
·
verified ·
1 Parent(s): 2997dde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,34 +1,44 @@
1
  import cv2
2
  import numpy as np
 
3
  import streamlit as st
 
4
  from camera_input_live import camera_input_live
5
 
6
- # Load Haarcascade for face detection
7
- cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
 
 
 
 
 
 
 
8
 
9
  # Streamlit app title
10
- st.title("Live Object Detection with Camera")
11
- st.subheader("Hold your face in front of the webcam to see real-time detection.")
12
 
13
  # Capture live camera input
14
  image = camera_input_live()
15
 
16
  if image is not None:
17
-
18
  # Convert the image to OpenCV format
19
  bytes_data = image.getvalue()
20
  cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
21
 
22
- # Convert to grayscale for face detection
23
- gray = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)
24
-
25
- # Detect faces in the image
26
- faces = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
27
 
28
- # Draw rectangles around detected faces
29
- for (x, y, w, h) in faces:
30
- cv2.rectangle(cv2_img, (x, y), (x + w, y + h), (0, 255, 0), 3)
31
- cv2.putText(cv2_img, "Face", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
 
 
 
 
 
32
 
33
  # Display the annotated image
34
- st.image(cv2_img, channels="BGR", caption="Detected Faces", use_container_width=True)
 
1
  import cv2
2
  import numpy as np
3
+ import torch
4
  import streamlit as st
5
+ from ultralytics import YOLO
6
  from camera_input_live import camera_input_live
7
 
8
+ # Load YOLO fire detection model
9
+ model_path = "last.pt"
10
+ if not torch.cuda.is_available():
11
+ device = "cpu"
12
+ else:
13
+ device = "cuda"
14
+
15
+ model = YOLO(model_path)
16
+ model.to(device)
17
 
18
  # Streamlit app title
19
+ st.title("Live Fire Detection with Camera")
20
+ st.subheader("Hold the camera towards potential fire sources to detect in real-time.")
21
 
22
  # Capture live camera input
23
  image = camera_input_live()
24
 
25
  if image is not None:
 
26
  # Convert the image to OpenCV format
27
  bytes_data = image.getvalue()
28
  cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), cv2.IMREAD_COLOR)
29
 
30
+ # Perform fire detection
31
+ results = model(cv2_img, device=device)
 
 
 
32
 
33
+ # Draw bounding boxes for detected fires
34
+ for result in results:
35
+ boxes = result.boxes
36
+ for box in boxes:
37
+ b = box.xyxy[0].cpu().numpy().astype(int)
38
+ c = int(box.cls[0])
39
+ label = f'Fire {box.conf[0]:.2f}'
40
+ cv2.rectangle(cv2_img, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
41
+ cv2.putText(cv2_img, label, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
42
 
43
  # Display the annotated image
44
+ st.image(cv2_img, channels="BGR", caption="Detected Fire", use_container_width=True)