File size: 4,079 Bytes
c0a5679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Serve live MJPEG streams from every connected camera over HTTP.

View from another machine on the network by opening http://<this-host-ip>:8000/ in a browser.
"""

import glob
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

import cv2

PORT = 8000
BOUNDARY = "frame"


def find_working_cameras() -> list[str]:
    working = []
    for path in sorted(glob.glob("/dev/video*")):
        cap = cv2.VideoCapture(path, cv2.CAP_V4L2)
        if cap.isOpened():
            ok, frame = cap.read()
            if ok and frame is not None:
                working.append(path)
        cap.release()
    return working


class CameraStream:
    def __init__(self, device_path: str) -> None:
        self.device_path = device_path
        self.cap = cv2.VideoCapture(device_path, cv2.CAP_V4L2)
        self.lock = threading.Lock()
        self.jpeg = b""
        self.running = True
        self.thread = threading.Thread(target=self._capture_loop, daemon=True)
        self.thread.start()

    def _capture_loop(self) -> None:
        while self.running:
            ok, frame = self.cap.read()
            if ok:
                ok2, buf = cv2.imencode(".jpg", frame)
                if ok2:
                    with self.lock:
                        self.jpeg = buf.tobytes()
            time.sleep(0.03)

    def get_jpeg(self) -> bytes:
        with self.lock:
            return self.jpeg

    def stop(self) -> None:
        self.running = False
        self.thread.join(timeout=1)
        self.cap.release()


streams: dict[str, CameraStream] = {}


class Handler(BaseHTTPRequestHandler):
    def log_message(self, format: str, *args) -> None:
        pass

    def do_GET(self) -> None:
        if self.path == "/":
            self._serve_index()
        elif self.path.startswith("/feed/"):
            name = self.path.removeprefix("/feed/")
            if name in streams:
                self._serve_feed(streams[name])
            else:
                self.send_error(404)
        else:
            self.send_error(404)

    def _serve_index(self) -> None:
        imgs = "".join(
            f'<div><h3>{name}</h3><img src="/feed/{name}" width="480"></div>' for name in streams
        )
        html = f"""<html><head><title>YAM camera test</title></head>
<body style="font-family: sans-serif; background:#111; color:#eee;">
<h2>Live camera feeds ({len(streams)} found)</h2>
<div style="display:flex; flex-wrap:wrap; gap:20px;">{imgs}</div>
</body></html>"""
        body = html.encode()
        self.send_response(200)
        self.send_header("Content-Type", "text/html")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def _serve_feed(self, stream: CameraStream) -> None:
        self.send_response(200)
        self.send_header("Content-Type", f"multipart/x-mixed-replace; boundary={BOUNDARY}")
        self.end_headers()
        try:
            while True:
                jpeg = stream.get_jpeg()
                if jpeg:
                    self.wfile.write(f"--{BOUNDARY}\r\n".encode())
                    self.wfile.write(b"Content-Type: image/jpeg\r\n")
                    self.wfile.write(f"Content-Length: {len(jpeg)}\r\n\r\n".encode())
                    self.wfile.write(jpeg)
                    self.wfile.write(b"\r\n")
                time.sleep(0.05)
        except (BrokenPipeError, ConnectionResetError):
            pass


def main() -> None:
    devices = find_working_cameras()
    print(f"found {len(devices)} working camera(s): {devices}")
    for path in devices:
        name = path.replace("/dev/", "")
        streams[name] = CameraStream(path)

    server = ThreadingHTTPServer(("0.0.0.0", PORT), Handler)
    print(f"serving on http://0.0.0.0:{PORT}/  (open http://<this-machine-ip>:{PORT}/ from another computer)")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        for s in streams.values():
            s.stop()


if __name__ == "__main__":
    main()