import io
import socket
import struct
import time
import numpy as np
import cv2

FPS = 1

# Connect a client socket to 192.168.0.106:8000

client_socket = socket.socket()
client_socket.connect(('192.168.100.59', 8000)) #replace with own address

# Make a file-like object out of the connection

connection = client_socket.makefile('wb')
try:
    # Note the start time and construct a stream to hold image data
    # temporarily
    start = time.time()
    cam = cv2.VideoCapture(0)
    time.sleep(2)
    while 1:
        ret, img = cam.read()
        connection.write(cv2.imencode('jpg',img)[1])
        time.sleep(1/FPS)
        # If we've been capturing for more than 30 seconds, quit
        if time.time() - start > 15:
            break
    # Write a length of zero to the stream to signal we're done
        connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()
