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