Last active
May 1, 2024 19:57
-
-
Save yorickdewid/c0e9e604a368306f2f706ce628cb006c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import cv2 | |
import os | |
import numpy as np | |
def display_image(data): | |
buffer = np.frombuffer(data, dtype="u1") | |
jpeg = cv2.imdecode(buffer, cv2.IMREAD_COLOR) | |
cv2.imshow("jpeg", jpeg) | |
cv2.waitKey(1) | |
import socket | |
sock = socket.socket(socket.AF_INET, # Internet | |
socket.SOCK_DGRAM) # UDP | |
sock.bind(("", 30061)) | |
while True: | |
data, addr = sock.recvfrom(1024 * 1024) # buffer size is 1024 bytes | |
print(f"received message with sz: {len(data)}") | |
buffer_begin_index = data.find(b"\xff\xd8") | |
buffer_end_index = data.find(b"\xff\xd9") | |
if buffer_begin_index != -1 and buffer_end_index != -1: | |
print("We have ze JPEG") | |
display_image(data) | |
#host = "10.8.0.240" # The server's hostname or IP address | |
#port = 30060 # The same port as used by the server | |
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
#s.connect((host, port)) | |
jpeg_buffer = b"" | |
i = 0 | |
while True: | |
data = s.recv(1024 * 1024) | |
if len(data) == 0: | |
break | |
#print("Received data size: ", len(data)) | |
buffer_begin_index = data.find(b"\xff\xd8") | |
if buffer_begin_index != -1: | |
if len(jpeg_buffer) > 0: | |
print("BUFFER NOT EMPTY") | |
print("JPEG Begin of Image") | |
jpeg_buffer = data[buffer_begin_index:] | |
elif len(jpeg_buffer) > 0: | |
jpeg_buffer += data | |
else: | |
print("No JPEG Begin of Image found") | |
continue | |
buffer_end_index = jpeg_buffer.find(b"\xff\xd9") | |
if buffer_end_index != -1: | |
print("JPEG End of Image") | |
#print("Saving frame ", i) | |
#with open(f"frame-{i}.raw", "wb") as f: | |
# f.write(jpeg_buffer[: buffer_end_index + 2]) | |
display_image(jpeg_buffer[: buffer_end_index + 2]) | |
jpeg_buffer = jpeg_buffer[buffer_end_index + 2 :] | |
i += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import cv2 | |
import os | |
import numpy as np | |
from PIL import Image | |
from linuxpy.video.device import Device, VideoCapture, VideoOutput, BufferType | |
# dev_source = Device.from_id(0) | |
# dev_source.open() | |
# print("Device info: ", dev_source.info.card) | |
# source = VideoCapture(dev_source) | |
# source.set_format(640, 480, "MJPG") | |
# source.open() | |
###### | |
import socket | |
host = "" # Symbolic name meaning all available interfaces | |
port = 12345 # Arbitrary non-privileged port | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((host, port)) | |
# con.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) | |
print("Socket bind complete") | |
while True: | |
print("Listening") | |
s.listen(1) | |
conn, addr = s.accept() | |
print("Connected by", addr) | |
# Open the device | |
dev_source = Device.from_id(0) | |
dev_source.open() | |
print("Device info: ", dev_source.info.card) | |
source = VideoCapture(dev_source) | |
source.set_format(640, 480, "MJPG") | |
source.open() | |
print("Device opened") | |
try: | |
for i, frame in enumerate(source): | |
# print("Saving frame ", i) | |
# with open(f"frame-{i}.raw", "wb") as f: | |
# f.write(bytes(frame)) | |
if frame[:2] == b"\xff\xd8": | |
print("JPEG Begin of Image") | |
scan_of_image = b"\xff\xda" | |
rs = bytes(frame).find(scan_of_image) | |
if rs != -1: | |
print("JPEG Start of Scan") | |
if frame[-2:] == b"\xff\xd9": | |
print("JPEG EOI") | |
print("Sending frame ", i) | |
frame_bytes = bytes(frame) | |
conn.sendall(frame_bytes) | |
except Exception as e: | |
print("Error: ", e) | |
source.close() | |
dev_source.close() | |
conn.close() | |
conn.close() | |
# | |
# | |
# | |
# | |
# | |
# for i, frame in enumerate(source): | |
# i2 = cv2.imdecode(frame.array, cv2.IMREAD_COLOR) | |
# cv2.imshow("i", i2) | |
# if cv2.waitKey(1) == 27: | |
# break | |
# fps = 30 | |
# def display_images_like_movie(image_dir, delay_ms): | |
# """Displays images from a directory in rapid succession. | |
# Args: | |
# image_dir (str): The path to the directory containing JPG images. | |
# delay_ms (int): The delay between images in milliseconds. Defaults to 15. | |
# """ | |
# image_files = [f for f in os.listdir(image_dir) if f.endswith(".raw")] | |
# image_files.sort(key=lambda x: int(x.split("-")[1].split(".")[0])) | |
# if not image_files: | |
# print("No JPG images found in the directory.") | |
# return | |
# for image_file in image_files: | |
# image_path = os.path.join(image_dir, image_file) | |
# image = cv2.imread(image_path) | |
# print("Displaying: ", image_file) | |
# cv2.imshow("Image Player", image) | |
# # Check if the user pressed 'q' to quit | |
# if cv2.waitKey(delay_ms) & 0xFF == ord("q"): | |
# break | |
# cv2.destroyAllWindows() | |
# image_directory = "/home/yorick/Projects/videotest" | |
# display_images_like_movie(image_directory, delay_ms=int(1000 / fps)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::Write; | |
use std::net::TcpListener; | |
use v4l::buffer::Type; | |
use v4l::io::traits::CaptureStream; | |
use v4l::prelude::*; | |
use v4l::video::Capture; | |
use v4l::Device; | |
use v4l::FourCC; | |
fn main() -> std::io::Result<()> { | |
let dev = Device::new(0).expect("Failed to open device"); | |
let caps = dev.query_caps(); | |
println!("Capabilities: {:?}", caps); | |
let mut fmt = dev.format().expect("Failed to read format"); | |
fmt.width = 640; | |
fmt.height = 480; | |
fmt.fourcc = FourCC::new(b"MJPG"); | |
let fmt = dev.set_format(&fmt).expect("Failed to write format"); | |
println!("Format in use:\n{}", fmt); | |
assert_eq!(fmt.fourcc, FourCC::new(b"MJPG")); | |
let listener = TcpListener::bind("0.0.0.0:30060")?; | |
for tcp_stream in listener.incoming() { | |
let mut tcp_stream = match tcp_stream { | |
Ok(stream) => stream, | |
Err(e) => { | |
eprintln!("Failed to accept connection: {}", e); | |
continue; | |
} | |
}; | |
println!("New connection: {}", tcp_stream.peer_addr()?); | |
let mut stream = MmapStream::with_buffers(&dev, Type::VideoCapture, 4) | |
.expect("Failed to create buffer stream"); | |
loop { | |
let (buf, meta) = stream.next().unwrap(); | |
println!( | |
"Buffer size: {}, seq: {}, timestamp: {} bytesused: {} flags: {:?} field: {}", | |
buf.len(), | |
meta.sequence, | |
meta.timestamp, | |
meta.bytesused, | |
meta.flags, | |
meta.field, | |
); | |
if let Err(e) = tcp_stream.write_all(&buf[..meta.bytesused as usize]) { | |
eprintln!("Failed to write to stream: {}", e); | |
break; | |
} | |
// let mut file = std::fs::File::create(format!("frame-{}.raw", meta.sequence)) | |
// .expect("Failed to create file"); | |
// file.write_all(&buf[..meta.bytesused as usize]) | |
// .expect("Failed to write to file"); | |
} | |
} | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment