Harika12323 commited on
Commit
78bdbf3
·
verified ·
1 Parent(s): ad7eba0

Update app.py

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