working
This commit is contained in:
65
video_methods.py
Normal file
65
video_methods.py
Normal file
@ -0,0 +1,65 @@
|
||||
import socket
|
||||
import struct
|
||||
import time
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from utils import recvall
|
||||
|
||||
methods = ["cam", "net"]
|
||||
|
||||
HOST = '0.0.0.0'
|
||||
PORT = 9999
|
||||
|
||||
class Method:
|
||||
def __init__(self, method_type):
|
||||
self.method_type = method_type
|
||||
|
||||
if method_type is "cam":
|
||||
self.cap = cv2.VideoCapture(0)
|
||||
|
||||
if not self.cap.isOpened():
|
||||
print("Nie można otworzyć kamerki")
|
||||
exit(1)
|
||||
else:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.bind((HOST, PORT))
|
||||
self.sock.listen(1)
|
||||
print(f"Oczekuje podłączenia na: {HOST}:{PORT}")
|
||||
self.conn, addr = self.sock.accept()
|
||||
print(f"Podłączono przez {addr}")
|
||||
|
||||
self.total_bytes_received = 0
|
||||
self.start_time = time.time()
|
||||
|
||||
def receive_frame(self):
|
||||
if self.method_type is "cam":
|
||||
_, frame = self.cap.read()
|
||||
|
||||
if not _:
|
||||
exit(1)
|
||||
else:
|
||||
packed_len = recvall(self.conn, 4)
|
||||
if not packed_len:
|
||||
exit(1)
|
||||
|
||||
length = struct.unpack('!I', packed_len)[0]
|
||||
|
||||
data = recvall(self.conn, length)
|
||||
if not data:
|
||||
exit(1)
|
||||
|
||||
self.total_bytes_received += length
|
||||
|
||||
img_array = np.frombuffer(data, dtype=np.uint8)
|
||||
frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
||||
|
||||
return frame
|
||||
|
||||
|
||||
def initialize_method(method_type):
|
||||
if not method_type in methods:
|
||||
return None
|
||||
|
||||
return Method(method_type)
|
||||
Reference in New Issue
Block a user