[Python] Please tell me how to use picamera2 and opencv2 together

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use Raspberry Pi (client) and computer (server) to send and receive images to and from each other

    First of all, the part where you send the video to the client -> server and the server outputs it is good

    I can't send and output images to the server -> client

    To see what's wrong with you

    I try to create code that is sent by the server and received by the client Maybe it's a problem with picamera2 and cv2, but when I import only cv2, I can output the video, but I can't output it just by adding picamera2

    I'm using picamera because I'm using a camera module, so please tell me how to solve this problem

    -send server-

    import socket
    import cv2
    import pickle
    import struct

    # 서버 IP 및 포트 설정
    server_ip = "0.0.0.0" # 클라이언트가 접속할 서버 IP
    server_port = 9999

    # TCP 서버 소켓 설정
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((server_ip, server_port))
    server_socket.listen(1)
    print("클라이언트 연결 대기 중...")
    client_socket, addr = server_socket.accept()
    print(f"클라이언트 연결됨: {addr}")

    # 웹캠 초기화 (640x480 해상도로 설정)
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    cap.set(cv2.CAP_PROP_FPS, 15) # FPS 제한

    try:
    while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
    print("프레임 캡처 실패!")
    break

    # 프레임을 직렬화
    data = pickle.dumps(frame)
    message_size = struct.pack("Q", len(data))

    # 데이터 전송
    client_socket.sendall(message_size + data)
    print(f"송신된 데이터 크기: {len(data)} 바이트")

    # 속도 조절 (15 FPS)
    cv2.waitKey(67)

    finally:
    client_socket.close()
    cap.release()
    server_socket.close()


    -recv client-

    import socket
    import cv2
    import pickle
    import struct
    #from picamera2 import Picamera2

    # 서버 IP 및 포트 설정
    server_ip = '192.168.1.100' # 서버의 IP 주소
    server_port = 9999

    # 소켓 설정
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((server_ip, server_port))

    def recv_exactly(sock, size):
    """ size 바이트만큼 정확히 수신하는 함수 """
    data = b""
    while len(data) < size:
    packet = sock.recv(size - len(data))
    if not packet:
    return None # 연결이 끊어지면 None 반환
    data += packet
    return data

    while True:
    try:
    # 데이터 크기 수신 (8바이트)
    data_size = recv_exactly(client_socket, 8)
    if not data_size:
    print("서버에서 데이터를 수신할 수 없습니다.")
    break

    # 수신한 데이터 크기 확인
    size = struct.unpack("Q", data_size)[0]
    print(f"수신된 데이터 크기: {size} 바이트")

    # 전체 데이터 수신 (끊기지 않도록 보장)
    data = recv_exactly(client_socket, size)
    if data is None:
    print("데이터 수신 실패!")
    break

    # 프레임 복원
    frame = pickle.loads(data)

    # 화면에 출력
    cv2.imshow("Video Stream", frame)

    # 종료 조건 (esc 키)
    if cv2.waitKey(33) & 0xFF == ord('q'):
    break

    except Exception as e:
    print(f"예외 발생: {e}")
    break

    # 연결 종료
    client_socket.close()
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página