amitkumar299 commited on
Commit
094bae9
·
verified ·
1 Parent(s): e1697b6

Upload 4 files

Browse files
Files changed (4) hide show
  1. main.py +42 -0
  2. requirements.txt +2 -0
  3. templates/index.html +30 -0
  4. yolov8n.pt +3 -0
main.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, Response
2
+ import cv2
3
+ from ultralytics import YOLO
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Load YOLOv8 model
8
+ model = YOLO("yolov8n.pt") # Make sure this file is present
9
+
10
+ # Start webcam
11
+ cap = cv2.VideoCapture(0)
12
+
13
+ def generate_frames():
14
+ while True:
15
+ success, frame = cap.read()
16
+ if not success:
17
+ break
18
+
19
+ # Object detection
20
+ results = model.predict(frame, conf=0.5)
21
+ annotated_frame = results[0].plot()
22
+
23
+ # Encode frame to JPEG
24
+ ret, buffer = cv2.imencode('.jpg', annotated_frame)
25
+ frame = buffer.tobytes()
26
+
27
+ # Stream frame
28
+ yield (b'--frame\r\n'
29
+ b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
30
+
31
+ # Main route with updated project name
32
+ @app.route('/')
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)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ultralytics
2
+ opencv-python
templates/index.html ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>{{ project_name }}</title>
6
+ <style>
7
+ body {
8
+ background-color: #000;
9
+ color: #00ffff;
10
+ text-align: center;
11
+ font-family: Arial, sans-serif;
12
+ }
13
+ h1 {
14
+ margin-top: 40px;
15
+ font-size: 2.5rem;
16
+ }
17
+ img {
18
+ margin-top: 30px;
19
+ border: 5px solid #00ffff;
20
+ border-radius: 12px;
21
+ max-width: 80%;
22
+ }
23
+ </style>
24
+ </head>
25
+ <body>
26
+ <h1>{{ project_name }}</h1>
27
+ <p style="color: #ccc;">✨ Real Time Object Detection</p>
28
+ <img src="{{ url_for('video_feed') }}" alt="Live Webcam Stream">
29
+ </body>
30
+ </html>
yolov8n.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
3
+ size 6549796