Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,68 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
import torch
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
'
|
22 |
-
'
|
23 |
-
'
|
24 |
-
'
|
25 |
-
'
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
'
|
46 |
-
'
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import yolov5
|
6 |
+
from yolov5 import YOLOv5
|
7 |
+
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Load YOLOv5 model
|
11 |
+
model = YOLOv5('yolov5s.pt') # Replace with your model path
|
12 |
+
|
13 |
+
def detect_number_plate(frame):
|
14 |
+
results = model(frame)
|
15 |
+
detections = results.pandas().xyxy[0]
|
16 |
+
plates = []
|
17 |
+
|
18 |
+
for _, row in detections.iterrows():
|
19 |
+
if row['name'] == 'number_plate': # Adjust class name
|
20 |
+
plates.append({
|
21 |
+
'class': row['name'],
|
22 |
+
'confidence': row['confidence'],
|
23 |
+
'x_min': row['xmin'],
|
24 |
+
'y_min': row['ymin'],
|
25 |
+
'x_max': row['xmax'],
|
26 |
+
'y_max': row['ymax']
|
27 |
+
})
|
28 |
+
|
29 |
+
return plates
|
30 |
+
|
31 |
+
def detect_smoke(frame):
|
32 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
33 |
+
blur = cv2.GaussianBlur(gray, (21, 21), 0)
|
34 |
+
_, thresh = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY)
|
35 |
+
|
36 |
+
smoke_intensity = np.sum(thresh) / (thresh.shape[0] * thresh.shape[1])
|
37 |
+
smoke_detected = smoke_intensity > 0.1 # Adjust this threshold
|
38 |
+
|
39 |
+
return smoke_detected, smoke_intensity
|
40 |
+
|
41 |
+
def process_frame(frame):
|
42 |
+
plates = detect_number_plate(frame)
|
43 |
+
smoke_detected, smoke_intensity = detect_smoke(frame)
|
44 |
+
return {
|
45 |
+
'smoke_detected': smoke_detected,
|
46 |
+
'smoke_intensity': smoke_intensity,
|
47 |
+
'number_plates': plates
|
48 |
+
}
|
49 |
+
|
50 |
+
@app.route('/upload', methods=['POST'])
|
51 |
+
def upload_file():
|
52 |
+
if 'file' not in request.files:
|
53 |
+
return jsonify({'error': 'No file part'}), 400
|
54 |
+
|
55 |
+
file = request.files['file']
|
56 |
+
|
57 |
+
if file.filename == '':
|
58 |
+
return jsonify({'error': 'No selected file'}), 400
|
59 |
+
|
60 |
+
if file:
|
61 |
+
in_memory_file = file.read()
|
62 |
+
np_arr = np.frombuffer(in_memory_file, np.uint8)
|
63 |
+
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
64 |
+
results = process_frame(frame)
|
65 |
+
return jsonify(results)
|
66 |
+
|
67 |
+
if __name__ == '__main__':
|
68 |
+
app.run(port=5000, debug=True)
|