Spaces:
Sleeping
Sleeping
init
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
from yolov5 import YOLOv5
|
| 6 |
+
|
| 7 |
+
# Initialize YOLOv5 model
|
| 8 |
+
model = YOLOv5('yolov5s.pt') # Replace with your model path
|
| 9 |
+
|
| 10 |
+
def detect_number_plate(frame):
|
| 11 |
+
results = model(frame)
|
| 12 |
+
detections = results.pandas().xyxy[0]
|
| 13 |
+
plates = []
|
| 14 |
+
|
| 15 |
+
for _, row in detections.iterrows():
|
| 16 |
+
if row['name'] == 'number_plate': # Adjust class name
|
| 17 |
+
plates.append({
|
| 18 |
+
'class': row['name'],
|
| 19 |
+
'confidence': row['confidence'],
|
| 20 |
+
'x_min': row['xmin'],
|
| 21 |
+
'y_min': row['ymin'],
|
| 22 |
+
'x_max': row['xmax'],
|
| 23 |
+
'y_max': row['ymax']
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
return plates
|
| 27 |
+
|
| 28 |
+
def detect_smoke(frame):
|
| 29 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 30 |
+
blur = cv2.GaussianBlur(gray, (21, 21), 0)
|
| 31 |
+
_, thresh = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)
|
| 32 |
+
|
| 33 |
+
smoke_intensity = np.sum(thresh) / (thresh.shape[0] * thresh.shape[1])
|
| 34 |
+
smoke_detected = smoke_intensity > 0.1 # Adjust this threshold
|
| 35 |
+
|
| 36 |
+
return smoke_detected, smoke_intensity
|
| 37 |
+
|
| 38 |
+
def process_frame(frame):
|
| 39 |
+
plates = detect_number_plate(frame)
|
| 40 |
+
smoke_detected, smoke_intensity = detect_smoke(frame)
|
| 41 |
+
return {
|
| 42 |
+
'smoke_detected': smoke_detected,
|
| 43 |
+
'smoke_intensity': smoke_intensity,
|
| 44 |
+
'number_plates': plates
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Streamlit app
|
| 48 |
+
st.title("Vehicle Number Plate and Smoke Detection")
|
| 49 |
+
|
| 50 |
+
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
|
| 51 |
+
|
| 52 |
+
if uploaded_file is not None:
|
| 53 |
+
# Convert file to image
|
| 54 |
+
in_memory_file = uploaded_file.read()
|
| 55 |
+
np_arr = np.frombuffer(in_memory_file, np.uint8)
|
| 56 |
+
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 57 |
+
|
| 58 |
+
# Process the frame
|
| 59 |
+
results = process_frame(frame)
|
| 60 |
+
|
| 61 |
+
st.subheader("Results")
|
| 62 |
+
st.write(f"Smoke Detected: {results['smoke_detected']}")
|
| 63 |
+
st.write(f"Smoke Intensity: {results['smoke_intensity']:.2f}")
|
| 64 |
+
|
| 65 |
+
st.subheader("Number Plates Detected")
|
| 66 |
+
for plate in results['number_plates']:
|
| 67 |
+
st.write(f"Class: {plate['class']}, Confidence: {plate['confidence']:.2f}")
|
| 68 |
+
st.write(f"Bounding Box: ({plate['x_min']}, {plate['y_min']}) to ({plate['x_max']}, {plate['y_max']})")
|