Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,32 @@
|
|
| 1 |
-
from flask import Flask, render_template,
|
| 2 |
-
import cv2
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model = YOLO("yolov8n.pt")
|
| 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 |
-
def index():
|
| 34 |
-
project_name = "✨ Real Time Object Detection"
|
| 35 |
-
return render_template("index.html", project_name=project_name)
|
| 36 |
-
|
| 37 |
-
@app.route('/video_feed')
|
| 38 |
-
def video_feed():
|
| 39 |
-
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 40 |
-
|
| 41 |
-
if __name__ == '__main__':
|
| 42 |
-
app.run(debug=False)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
model = YOLO("yolov8n.pt")
|
| 9 |
+
|
| 10 |
+
def readb64(base64_string):
|
| 11 |
+
nparr = np.frombuffer(base64.b64decode(base64_string.split(',')[1]), np.uint8)
|
| 12 |
+
return cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 13 |
+
|
| 14 |
+
@app.route('/')
|
| 15 |
+
def index():
|
| 16 |
+
return render_template('index.html')
|
| 17 |
+
|
| 18 |
+
@app.route('/detect', methods=['POST'])
|
| 19 |
+
def detect():
|
| 20 |
+
data = request.get_json()
|
| 21 |
+
frame_data = data['image']
|
| 22 |
+
|
| 23 |
+
frame = readb64(frame_data)
|
| 24 |
+
results = model.predict(frame, conf=0.5)
|
| 25 |
+
annotated = results[0].plot()
|
| 26 |
+
|
| 27 |
+
_, buffer = cv2.imencode('.jpg', annotated)
|
| 28 |
+
annotated_b64 = base64.b64encode(buffer).decode('utf-8')
|
| 29 |
+
return jsonify({'image': f'data:image/jpeg;base64,{annotated_b64}'})
|
| 30 |
+
|
| 31 |
+
if __name__ == '__main__':
|
| 32 |
+
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|