Skip to content

Commit 67e3c30

Browse files
author
RobinRosculete
committed
Added 2d model real time stream
1 parent 35d1475 commit 67e3c30

File tree

7 files changed

+152
-57
lines changed

7 files changed

+152
-57
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
**/node_modules
22
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
33

4+
# Virtual enviorment for server
5+
.venv/
6+
.bin/
7+
48
# dependencies
59
/.pnp
610
.pnp.js

model/2dModel.pt

21.5 MB
Binary file not shown.

server/server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from flask import Flask, render_template, Response
2+
from webcam import gen_frames
3+
4+
5+
# Initializing flask app
6+
app = Flask(__name__)
7+
8+
9+
@app.route('/webcam')
10+
def video_feed():
11+
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
12+
13+
# Running app
14+
if __name__ == '__main__':
15+
app.run(debug=True)

server/webcam.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#Thanks to https://dipankarmedh1.medium.com/real-time-object-detection-with-yolo-and-webcam-enhancing-your-computer-vision-skills-861b97c78993
2+
import cv2
3+
from ultralytics import YOLO
4+
import math
5+
6+
7+
camera = cv2.VideoCapture(0)
8+
9+
PATH_TO_MODEL = "/Users/Robin1/Desktop/rttm/model/2dModel.pt"
10+
#model = YOLO('yolov8s.pt') # load an official model
11+
model = YOLO(PATH_TO_MODEL )
12+
# object classes
13+
classNames = [
14+
'car',
15+
'pickup',
16+
'SUV',
17+
'van',
18+
'truck',
19+
'bus',
20+
'motorcycle',
21+
'pedestrian'
22+
]
23+
24+
def gen_frames():
25+
26+
while True:
27+
success, frame = camera.read() # read the camera frame
28+
results = model(frame, stream=True)#
29+
# coordinates
30+
for r in results:
31+
boxes = r.boxes
32+
33+
for box in boxes:
34+
# bounding box
35+
x1, y1, x2, y2 = box.xyxy[0]
36+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
37+
38+
# put box in cam
39+
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 255), 3)
40+
41+
# confidence
42+
confidence = math.ceil((box.conf[0]*100))/100
43+
print("Confidence --->",confidence)
44+
45+
# class name
46+
cls = int(box.cls[0])
47+
print("Class name -->", classNames[cls])
48+
49+
# object details
50+
org = [x1, y1]
51+
font = cv2.FONT_HERSHEY_SIMPLEX
52+
fontScale = 1
53+
color = (255, 0, 0)
54+
thickness = 2
55+
cv2.putText(frame, classNames[cls], org, font, fontScale, color, thickness)
56+
57+
if not success:
58+
break
59+
else:
60+
ret, buffer = cv2.imencode('.jpg', frame)
61+
62+
frame = buffer.tobytes()
63+
yield (b'--frame\r\n'
64+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result

website/src/components/Mainpage.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
//import Chart from "react-apexcharts";
2-
import React from "react";
3-
import StackedArea from './subcomponents/sub-graph/StackedArea';
4-
import Bar from './subcomponents/sub-graph/Bar';
5-
import PieChart from './subcomponents/sub-graph/PieChart';
6-
import './subcomponents/sub-graph/charts.css';
7-
import './subcomponents/sub-s3-components/videoPlayer.css';
2+
3+
import React, { useEffect, useRef } from "react";
4+
import StackedArea from "./subcomponents/sub-graph/StackedArea";
5+
import Bar from "./subcomponents/sub-graph/Bar";
6+
import PieChart from "./subcomponents/sub-graph/PieChart";
7+
import "./subcomponents/sub-graph/charts.css";
8+
import "./subcomponents/sub-s3-components/videoPlayer.css";
89
import StackedBar from "./subcomponents/sub-graph/StackedBar";
910
import Density from "./subcomponents/sub-graph/Density";
10-
import '../index.css';
11+
import "../index.css";
1112
// testhomepage
1213
const Mainpage = () => {
14+
const videoRef = useRef(null);
15+
useEffect(() => {
16+
const fetchWebcamStream = async () => {
17+
try {
18+
const videoElement = videoRef.current;
19+
20+
const stream = await navigator.mediaDevices.getUserMedia({
21+
video: true,
22+
});
23+
24+
if (videoElement && stream) {
25+
videoElement.srcObject = stream;
26+
}
27+
} catch (error) {
28+
console.error("Error accessing webcam stream:", error);
29+
}
30+
};
31+
32+
fetchWebcamStream();
33+
}, []);
1334
// Page Layout
1435
return (
1536
<section>
@@ -30,27 +51,19 @@ const Mainpage = () => {
3051
poster="YOUR_CAMERA1_POSTER.jpg" // Change to your Camera 1 poster image path
3152
data-setup="{}"
3253
>
33-
<source src="../videos/YOLOv7-Tiny Demo.mp4" type="video/mp4" /> {/* Change to your Camera 1 video path */}
54+
<source src="../videos/YOLOv7-Tiny Demo.mp4" type="video/mp4" />{" "}
55+
{/* Change to your Camera 1 video path */}
3456
</video>
3557
</div>
3658
</div>
3759
<div className="col">
38-
<h4 className="camText gradient-label">Camera 2</h4>
60+
<h4 className="camText gradient-label">Camera 2 (2D)</h4>
3961
<div className="video-box">
40-
<video
41-
id="camera2-video"
42-
className="video-js"
43-
autoPlay
44-
muted
45-
loop
46-
preload="auto"
47-
width="100%"
48-
height="100%"
49-
poster="MY_VIDEO_POSTER.jpg"
50-
data-setup="{}"
51-
>
52-
<source src="../videos/YOLOv7-Tiny Demo.mp4" type="video/mp4" />
53-
</video>
62+
<img
63+
src="http://127.0.0.1:5000/webcam"
64+
alt="webcam"
65+
className="webcam-image"
66+
/>
5467
</div>
5568
</div>
5669
</div>
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
.video-box {
2-
position: relative;
3-
height: 20em; /* Reduced from 26.2em to make the box smaller in height */
4-
text-align: center;
2+
border: 2px solid #ccc;
3+
border-radius: 8px;
4+
overflow: hidden;
55
width: 100%;
6-
margin-top: 3pt; /* Adjusted from 5pt for tighter spacing */
7-
margin-bottom: 3pt; /* Adjusted from 5pt for tighter spacing */
8-
background-color: rgb(128, 128, 128);
9-
min-height: auto;
10-
min-width: none;
11-
padding: 1pt; /* Reduced padding */
12-
color: rgb(216, 216, 216);
6+
height: 400px; /* Adjust height as needed */
7+
}
8+
9+
.webcam-image {
10+
width: 60%;
11+
height: 100%;
12+
object-fit: cover; /* Maintain aspect ratio and cover entire container */
1313
}

website/src/index.css

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
body {
44
background-color: black;
55
margin: 0;
6-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
7-
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
6+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
7+
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
88
sans-serif;
99
-webkit-font-smoothing: antialiased;
1010
-moz-osx-font-smoothing: grayscale;
1111
}
1212

1313
/* Header style */
1414
header {
15-
background-color: #43404F; /* Dark gray color */
15+
background-color: #43404f; /* Dark gray color */
1616
padding: 10px 0;
1717
text-align: center;
1818
color: white;
19-
font-size: 24px; /* Large text for the logo */
19+
font-size: 24px; /* Large text for the logo */
2020
}
2121

2222
/* Footer style */
2323
footer {
24-
background-color: rgb(37, 37, 37); /* Dark gray color */
24+
background-color: rgb(37, 37, 37); /* Dark gray color */
2525
position: absolute;
2626
left: 0;
2727
bottom: 0;
@@ -31,19 +31,19 @@ footer {
3131
color: white;
3232
}
3333
code {
34-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
34+
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
3535
monospace;
3636
}
3737

38-
.background{
38+
.background {
3939
height: 100%;
4040
width: 100%;
41-
background:linear-gradient(rgba(0, 0, 0, 0.d),transparent);
42-
background-color:#565264 /*this your primary color*/
41+
background: linear-gradient(rgba(0, 0, 0, 0d), transparent);
42+
background-color: #565264; /*this your primary color*/
4343
}
4444

4545
/* This box below has width that reaches the entire div but its height is based on the content */
46-
.box{
46+
.box {
4747
position: relative;
4848
width: 100%;
4949
margin-top: 5pt;
@@ -59,11 +59,10 @@ code {
5959
/* Box border and background shadow */
6060
border-radius: 5px;
6161
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.5);
62-
6362
}
6463

6564
/* This below has a defined dimension which is why the text is fully covered with space */
66-
.text-box{
65+
.text-box {
6766
position: relative;
6867
height: 23.2em;
6968
text-align: center;
@@ -82,15 +81,14 @@ code {
8281
}
8382

8483
/*this is for the description section of the web page*/
85-
.RTTMDescription{
86-
font-size:13pt;
87-
84+
.RTTMDescription {
85+
font-size: 13pt;
8886
}
89-
.section{
87+
.section {
9088
background-color: black;
9189
}
92-
.welcome{
93-
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
90+
.welcome {
91+
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
9492
padding-top: 10pt;
9593
padding-bottom: 3pt;
9694
color: rgb(248, 248, 248);
@@ -105,8 +103,6 @@ code {
105103
width: 100%;
106104
}
107105

108-
109-
110106
.camera-container {
111107
display: flex;
112108
justify-content: center; /* Center horizontally */
@@ -115,7 +111,7 @@ code {
115111
}
116112

117113
.camera-button {
118-
background: linear-gradient(to right, #56CCF2, #2F80ED); /* Blue gradient */
114+
background: linear-gradient(to right, #56ccf2, #2f80ed); /* Blue gradient */
119115
color: white; /* Text color */
120116
padding: 5px 10px; /* Smaller padding */
121117
border: none; /* No border */
@@ -131,9 +127,12 @@ code {
131127
transform: scale(0.95); /* Scales down the button a bit when hovered over */
132128
}
133129

134-
135130
.gradient-label {
136-
background: linear-gradient(to right, #d22030, #565264); /* Gradient background */
131+
background: linear-gradient(
132+
to right,
133+
#d22030,
134+
#565264
135+
); /* Gradient background */
137136
color: white; /* Text color */
138137
padding: 1px 10px; /* Vertical padding is 1px, horizontal padding is 10px */
139138
border-radius: 5px; /* Rounded corners */
@@ -147,6 +146,6 @@ code {
147146

148147
.echarts-container {
149148
height: 300px; /* Set the height you want for all charts */
150-
width: 300px; /* Set the width you want for all charts */
149+
width: 300px; /* Set the width you want for all charts */
151150
/* Add any other styles you want to apply globally */
152151
}

0 commit comments

Comments
 (0)