Spaces:
Sleeping
Sleeping
File size: 1,778 Bytes
f9f0179 bd7bf66 f9f0179 7a163d4 |
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from flask import Flask, Response, render_template, request
import torch
from ultralytics import YOLO
import cv2
import numpy as np
app = Flask(__name__)
# Load YOLOv8 model
model = YOLO("./yolov8x.pt") # Ensure the model is in the root directory
# Store user-provided IP Webcam URL
IP_WEBCAM_URL = None
def generate_frames():
global IP_WEBCAM_URL
if not IP_WEBCAM_URL:
return
cap = cv2.VideoCapture(IP_WEBCAM_URL)
while True:
success, frame = cap.read()
if not success:
break
# Perform YOLOv8 object detection
results = model.predict(frame)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
label = result.names[int(box.cls[0])]
conf = box.conf[0].item()
# Draw bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f"{label} {conf:.2f}", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Encode frame as JPEG
_, buffer = cv2.imencode('.jpg', frame)
frame_bytes = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
cap.release()
@app.route('/', methods=['GET', 'POST'])
def index():
global IP_WEBCAM_URL
if request.method == 'POST':
IP_WEBCAM_URL = request.form['ip_url']
return render_template('index.html', ip_url=IP_WEBCAM_URL)
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080)
|