Spaces:
Runtime error
Runtime error
dwkurnie
commited on
Commit
·
9396a48
1
Parent(s):
e30a20d
Add application file
Browse files- app.py +39 -5
- templates/index.html +14 -0
- yolov5/weights/model5.pt +3 -0
app.py
CHANGED
@@ -1,7 +1,41 @@
|
|
1 |
-
from
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, Response
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
|
5 |
+
import pathlib
|
6 |
+
temp = pathlib.PosixPath
|
7 |
+
pathlib.PosixPath = pathlib.WindowsPath
|
8 |
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# Load custom YOLOv5 model
|
12 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/weights/model5.pt')
|
13 |
+
|
14 |
+
def gen_frames():
|
15 |
+
cap = cv2.VideoCapture(0)
|
16 |
+
while True:
|
17 |
+
success, frame = cap.read()
|
18 |
+
if not success:
|
19 |
+
break
|
20 |
+
else:
|
21 |
+
# Perform object detection
|
22 |
+
results = model(frame)
|
23 |
+
frame = results.render()[0]
|
24 |
+
|
25 |
+
# Encode frame as JPEG
|
26 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
27 |
+
frame = buffer.tobytes()
|
28 |
+
|
29 |
+
yield (b'--frame\r\n'
|
30 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
31 |
+
|
32 |
+
@app.route('/')
|
33 |
+
def index():
|
34 |
+
return render_template('index.html')
|
35 |
+
|
36 |
+
@app.route('/video_feed')
|
37 |
+
def video_feed():
|
38 |
+
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
39 |
+
|
40 |
+
if __name__ == '__main__':
|
41 |
+
app.run(debug=True)
|
templates/index.html
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Webcam Object Detection</title>
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<h1>Webcam Object Detection using YOLOv5</h1>
|
10 |
+
<div>
|
11 |
+
<img id="video_feed" src="{{ url_for('video_feed') }}">
|
12 |
+
</div>
|
13 |
+
</body>
|
14 |
+
</html>
|
yolov5/weights/model5.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fbaf9114cbf059a59817f69db3e0ba50202e0fa6b6b5caf8c94329df38d10698
|
3 |
+
size 14471080
|