-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprueba.py
More file actions
44 lines (36 loc) · 1.49 KB
/
prueba.py
File metadata and controls
44 lines (36 loc) · 1.49 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
import cv2
import threading
import http.server
import socketserver
# Inicializa el objeto VideoCapture con la cámara web por defecto
cap = cv2.VideoCapture(0)
# Configura el ancho y alto del frame a transmitir
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Inicializa el codec y crea el objeto VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter('http://localhost:8080/live/stream', fourcc, 20.0, (640, 480))
# Clase para manejar las solicitudes HTTP
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/live/stream':
self.send_response(200)
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=frame')
self.end_headers()
while True:
# Lee el siguiente frame de la cámara
ret, frame = cap.read()
if not ret:
break
# Escribir el frame a la salida
out.write(frame)
# Enviar el frame al cliente
self.wfile.write("--frame\r\n")
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write("\r\n")
# Inicia el servidor HTTP
httpd = socketserver.TCPServer (("", 8080), Handler)
httpd.serve_forever()