Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, send_file, Response
|
| 2 |
+
from werkzeug.utils import secure_filename
|
| 3 |
+
import io
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import cv2
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
app.config['UPLOAD_FOLDER'] = 'uploads/'
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class Detection:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
#download weights from here:https://github.com/ultralytics/ultralytics and change the path
|
| 17 |
+
self.model = YOLO(r"best.pt")
|
| 18 |
+
|
| 19 |
+
def predict(self, img, classes=[], conf=0.5):
|
| 20 |
+
if classes:
|
| 21 |
+
results = self.model.predict(img, classes=classes, conf=conf)
|
| 22 |
+
else:
|
| 23 |
+
results = self.model.predict(img, conf=conf)
|
| 24 |
+
|
| 25 |
+
return results
|
| 26 |
+
|
| 27 |
+
def predict_and_detect(self, img, classes=[], conf=0.5, rectangle_thickness=2, text_thickness=1):
|
| 28 |
+
results = self.predict(img, classes, conf=conf)
|
| 29 |
+
for result in results:
|
| 30 |
+
for box in result.boxes:
|
| 31 |
+
cv2.rectangle(img, (int(box.xyxy[0][0]), int(box.xyxy[0][1])),
|
| 32 |
+
(int(box.xyxy[0][2]), int(box.xyxy[0][3])), (255, 0, 0), rectangle_thickness)
|
| 33 |
+
cv2.putText(img, f"{result.names[int(box.cls[0])]}",
|
| 34 |
+
(int(box.xyxy[0][0]), int(box.xyxy[0][1]) - 10),
|
| 35 |
+
cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), text_thickness)
|
| 36 |
+
return img, results
|
| 37 |
+
|
| 38 |
+
def detect_from_image(self, image):
|
| 39 |
+
result_img, _ = self.predict_and_detect(image, classes=[], conf=0.5)
|
| 40 |
+
return result_img
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
detection = Detection()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@app.route('/')
|
| 47 |
+
def index():
|
| 48 |
+
return render_template('index.html')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@app.route('/object-detection/', methods=['POST'])
|
| 52 |
+
def apply_detection():
|
| 53 |
+
if 'image' not in request.files:
|
| 54 |
+
return 'No file part'
|
| 55 |
+
|
| 56 |
+
file = request.files['image']
|
| 57 |
+
if file.filename == '':
|
| 58 |
+
return 'No selected file'
|
| 59 |
+
|
| 60 |
+
if file:
|
| 61 |
+
filename = secure_filename(file.filename)
|
| 62 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 63 |
+
file.save(file_path)
|
| 64 |
+
|
| 65 |
+
img = Image.open(file_path).convert("RGB")
|
| 66 |
+
img = np.array(img)
|
| 67 |
+
img = cv2.resize(img, (512, 512))
|
| 68 |
+
img = detection.detect_from_image(img)
|
| 69 |
+
output = Image.fromarray(img)
|
| 70 |
+
|
| 71 |
+
buf = io.BytesIO()
|
| 72 |
+
output.save(buf, format="PNG")
|
| 73 |
+
buf.seek(0)
|
| 74 |
+
|
| 75 |
+
os.remove(file_path)
|
| 76 |
+
return send_file(buf, mimetype='image/png')
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
@app.route('/video')
|
| 80 |
+
def index_video():
|
| 81 |
+
return render_template('video.html')
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def gen_frames():
|
| 85 |
+
cap = cv2.VideoCapture(0)
|
| 86 |
+
while cap.isOpened():
|
| 87 |
+
ret, frame = cap.read()
|
| 88 |
+
frame = cv2.resize(frame, (512, 512))
|
| 89 |
+
if frame is None:
|
| 90 |
+
break
|
| 91 |
+
frame = detection.detect_from_image(frame)
|
| 92 |
+
|
| 93 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
| 94 |
+
frame = buffer.tobytes()
|
| 95 |
+
|
| 96 |
+
yield (b'--frame\r\n'
|
| 97 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@app.route('/video_feed')
|
| 101 |
+
def video_feed():
|
| 102 |
+
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
| 103 |
+
|
| 104 |
+
if __name__ == '__main__':
|
| 105 |
+
app.run(host="0.0.0.0", port=5000)
|
| 106 |
+
#http://localhost:8000/video for video source
|
| 107 |
+
#http://localhost:8000 for image source
|