Prathamesh1420 commited on
Commit
b3e6c70
·
verified ·
1 Parent(s): 35eb4d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +279 -0
app.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''from flask import Flask, render_template, request
2
+ import cv2
3
+ import numpy as np
4
+ import base64
5
+ import re
6
+
7
+ app = Flask(__name__)
8
+
9
+ @app.route('/')
10
+ def index():
11
+ return render_template('index.html')
12
+
13
+ @app.route('/upload_frame', methods=['POST'])
14
+ def upload_frame():
15
+ data = request.get_json()
16
+ if 'image' not in data:
17
+ return 'No image', 400
18
+
19
+ image_data = re.sub('^data:image/.+;base64,', '', data['image'])
20
+ img_bytes = base64.b64decode(image_data)
21
+ np_img = np.frombuffer(img_bytes, dtype=np.uint8)
22
+ frame = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
23
+
24
+ # Process frame here (e.g., face detection)
25
+ print("Received a frame of shape:", frame.shape)
26
+
27
+ return 'OK', 200
28
+
29
+ if __name__ == '__main__':
30
+ app.run(host='0.0.0.0', port=5001, debug=True)'''
31
+ '''
32
+ import cv2
33
+ import numpy as np
34
+ import base64
35
+ from flask import Flask, request, jsonify, render_template
36
+ from ultralytics import YOLO
37
+
38
+ app = Flask(__name__)
39
+ model = YOLO('yolov8n.pt') # Load the YOLO model
40
+
41
+ @app.route('/')
42
+ def index():
43
+ return render_template('index.html') # Your HTML file
44
+
45
+ @app.route('/upload_frame', methods=['POST'])
46
+ def upload_frame():
47
+ data = request.get_json()
48
+ image_data = data['image'].split(',')[1]
49
+ img_bytes = base64.b64decode(image_data)
50
+ np_arr = np.frombuffer(img_bytes, np.uint8)
51
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
52
+
53
+ # Run YOLO detection
54
+ results = model(frame)
55
+ for result in results:
56
+ for box in result.boxes:
57
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
58
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
59
+
60
+ # (Optional) Save or return detections if needed
61
+ return jsonify({"status": "success"})
62
+
63
+ if __name__ == '__main__':
64
+ app.run(debug=True)
65
+ '''
66
+ '''import cv2
67
+ import numpy as np
68
+ import base64
69
+ from flask import Flask, request, jsonify, render_template
70
+ from ultralytics import YOLO
71
+
72
+ app = Flask(__name__)
73
+ model = YOLO('yolov8n.pt')
74
+
75
+ @app.route('/')
76
+ def index():
77
+ return render_template('index.html')
78
+
79
+ @app.route('/upload_frame', methods=['POST'])
80
+ def upload_frame():
81
+ data = request.get_json()
82
+ image_data = data['image'].split(',')[1]
83
+ img_bytes = base64.b64decode(image_data)
84
+ np_arr = np.frombuffer(img_bytes, np.uint8)
85
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
86
+
87
+ # Run YOLO detection
88
+ results = model(frame)
89
+ for result in results:
90
+ for box in result.boxes:
91
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
92
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
93
+
94
+ # Encode the frame back to JPEG
95
+ _, buffer = cv2.imencode('.jpg', frame)
96
+ annotated_base64 = base64.b64encode(buffer).decode('utf-8')
97
+ return jsonify({"status": "success", "image": f"data:image/jpeg;base64,{annotated_base64}"})
98
+
99
+ if __name__ == '__main__':
100
+ app.run(host="0.0.0.0", port=5001, debug=True)'''
101
+ '''import cv2
102
+ import numpy as np
103
+ import base64
104
+ from flask import Flask, request, jsonify, render_template
105
+ from ultralytics import YOLO
106
+
107
+ app = Flask(__name__)
108
+
109
+ # Load YOLOv8 model (e.g., yolov8n.pt, yolov8s.pt, etc.)
110
+ model = YOLO('yolov8n.pt')
111
+
112
+ @app.route('/')
113
+ def index():
114
+ return render_template('index.html')
115
+
116
+ @app.route('/upload_frame', methods=['POST'])
117
+ def upload_frame():
118
+ data = request.get_json()
119
+ if 'image' not in data:
120
+ return jsonify({'error': 'No image provided'}), 400
121
+
122
+ # Decode base64 image
123
+ image_data = data['image'].split(',')[1]
124
+ img_bytes = base64.b64decode(image_data)
125
+ np_arr = np.frombuffer(img_bytes, np.uint8)
126
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
127
+
128
+ # Run YOLO detection
129
+ results = model(frame)
130
+
131
+ for result in results:
132
+ for box in result.boxes:
133
+ x1, y1, x2, y2 = map(int, box.xyxy[0]) # Coordinates
134
+ class_id = int(box.cls[0]) # Class ID
135
+ class_name = model.names[class_id] # Class name
136
+
137
+ # Draw bounding box
138
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
139
+
140
+ # Put class name text
141
+ cv2.putText(
142
+ frame,
143
+ class_name,
144
+ (x1, y1 - 10),
145
+ cv2.FONT_HERSHEY_SIMPLEX,
146
+ 0.6,
147
+ (0, 255, 0),
148
+ 2
149
+ )
150
+
151
+ # Encode annotated image back to base64
152
+ _, buffer = cv2.imencode('.jpg', frame)
153
+ annotated_base64 = base64.b64encode(buffer).decode('utf-8')
154
+
155
+ return jsonify({
156
+ "status": "success",
157
+ "image": f"data:image/jpeg;base64,{annotated_base64}"
158
+ })
159
+
160
+ if __name__ == '__main__':
161
+ app.run(host='0.0.0.0', port=5001, debug=True)'''
162
+
163
+ '''import cv2
164
+ import numpy as np
165
+ import base64
166
+ from flask import Flask, request, jsonify, render_template
167
+ from ultralytics import YOLO
168
+
169
+ app = Flask(__name__)
170
+ model = YOLO('yolov8n.pt') # Load YOLOv8 model
171
+ CONFIDENCE_THRESHOLD = 0.8 # Set confidence threshold (0.0 to 1.0)
172
+
173
+ @app.route('/')
174
+ def index():
175
+ return render_template('index.html')
176
+
177
+ @app.route('/upload_frame', methods=['POST'])
178
+ def upload_frame():
179
+ data = request.get_json()
180
+ if 'image' not in data:
181
+ return jsonify({'error': 'No image provided'}), 400
182
+
183
+ # Decode base64 image
184
+ image_data = data['image'].split(',')[1]
185
+ img_bytes = base64.b64decode(image_data)
186
+ np_arr = np.frombuffer(img_bytes, np.uint8)
187
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
188
+
189
+ # Run YOLO detection
190
+ results = model(frame)
191
+
192
+ for result in results:
193
+ for box in result.boxes:
194
+ if box.conf[0] < CONFIDENCE_THRESHOLD:
195
+ continue
196
+
197
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
198
+ class_id = int(box.cls[0])
199
+ class_name = model.names[class_id]
200
+ confidence = float(box.conf[0])
201
+
202
+ # Draw bounding box
203
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
204
+
205
+ # Draw class name + confidence
206
+ label = f"{class_name} {confidence:.2f}"
207
+ cv2.putText(frame, label, (x1, y1 - 10),
208
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
209
+
210
+ # Encode the frame back to base64
211
+ _, buffer = cv2.imencode('.jpg', frame)
212
+ annotated_base64 = base64.b64encode(buffer).decode('utf-8')
213
+
214
+ return jsonify({
215
+ "status": "success",
216
+ "image": f"data:image/jpeg;base64,{annotated_base64}"
217
+ })
218
+
219
+ if __name__ == '__main__':
220
+ app.run(host='0.0.0.0', port=5001, debug=True)
221
+ '''
222
+ import cv2
223
+ import numpy as np
224
+ import base64
225
+ from flask import Flask, request, jsonify, render_template
226
+ from ultralytics import YOLO
227
+
228
+ app = Flask(__name__)
229
+ model = YOLO('yolov8n.pt') # Replace with your desired YOLO model
230
+ CONFIDENCE_THRESHOLD = 0.5
231
+
232
+ @app.route('/')
233
+ def index():
234
+ return render_template('index.html')
235
+
236
+ @app.route('/upload_frame', methods=['POST'])
237
+ def upload_frame():
238
+ data = request.get_json()
239
+ if 'image' not in data:
240
+ return jsonify({'error': 'No image provided'}), 400
241
+
242
+ # Decode base64 image
243
+ image_data = data['image'].split(',')[1]
244
+ img_bytes = base64.b64decode(image_data)
245
+ np_arr = np.frombuffer(img_bytes, np.uint8)
246
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
247
+
248
+ # Run YOLO detection
249
+ results = model(frame)
250
+
251
+ for result in results:
252
+ for box in result.boxes:
253
+ if box.conf[0] < CONFIDENCE_THRESHOLD:
254
+ continue
255
+
256
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
257
+ class_id = int(box.cls[0])
258
+ class_name = model.names[class_id]
259
+ confidence = float(box.conf[0])
260
+
261
+ # Draw bounding box
262
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
263
+
264
+ # Draw class name + confidence
265
+ label = f"{class_name} {confidence:.2f}"
266
+ cv2.putText(frame, label, (x1, y1 - 10),
267
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
268
+
269
+ # Encode the frame back to base64
270
+ _, buffer = cv2.imencode('.jpg', frame)
271
+ annotated_base64 = base64.b64encode(buffer).decode('utf-8')
272
+
273
+ return jsonify({
274
+ "status": "success",
275
+ "image": f"data:image/jpeg;base64,{annotated_base64}"
276
+ })
277
+
278
+ if __name__ == '__main__':
279
+ app.run(host='0.0.0.0', port=5001, debug=True)