-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.py
More file actions
67 lines (58 loc) · 1.88 KB
/
Copy pathweb_server.py
File metadata and controls
67 lines (58 loc) · 1.88 KB
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
65
66
67
from flask import Flask, Response
from cv2 import CAP_FFMPEG, Mat, VideoCapture, imencode, CAP_PROP_FOURCC, CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT, VideoWriter_fourcc
app = Flask(__name__)
cap = VideoCapture(0)
cap.set(CAP_PROP_FOURCC, VideoWriter_fourcc(*'MJPG'))
def gen(camera: VideoCapture):
while True:
ret, raw_frame = camera.read()
if not ret:
break
_, buffer = imencode(".jpg", raw_frame)
frame = buffer.tobytes()
if ret:
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
@app.route("/video_feed")
def video_feed():
return Response(gen(cap), mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route("/")
def index():
"""Home page with embedded video stream"""
return """
<html>
<head>
<title>Camera Stream</title>
<style>
body {
margin: 0;
padding: 20px;
background-color: #1e1e1e;
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
}
h1 {
color: #ffffff;
}
img {
border: 2px solid #444;
border-radius: 8px;
max-width: 90%;
}
</style>
</head>
<body>
<h1>Live Camera Feed</h1>
<img src="/video_feed" alt="Camera Stream">
</body>
</html>
"""
if __name__ == "__main__":
print("Starting camera web server...")
print("Open your browser and go to: http://localhost:5000")
try:
app.run(host="0.0.0.0", port=5000, debug=False, threaded=True)
finally:
cap.release()
print("Camera released")