'Sending webcam video and screen sharing at the same time using python, opencv

I have to make a project that shares screen and webcam video of multiple clients at the same time. The screen sharing and webcam sharing work separately but I can't combine them and make them work at the same time. I copied and pasted the code and then made changes to it, so I don't understand all of the code that well.

Server:

import socket, cv2, pickle, struct
import imutils
import threading
import pyshine as ps
import cv2

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print('HOST IP:', host_ip)
port = 9999
socket_address = (host_ip, port)
server_socket.bind(socket_address)
server_socket.listen()
print("Listening at", socket_address)


def show_client(addr, client_socket):
    try:
        print('CLIENT {} CONNECTED!'.format(addr))
        if client_socket:  # if a client socket exists
            data = b""
            payload_size = struct.calcsize("Q")
            while True:
                while len(data) < payload_size:
                    packet = client_socket.recv(4 * 1024)  # 4K
                    if not packet: break
                    data += packet
                packed_msg_size = data[:payload_size]
                data = data[payload_size:]
                msg_size = struct.unpack("Q", packed_msg_size)[0]

                while len(data) < msg_size:
                    data += client_socket.recv(4 * 1024)
                frame_data = data[:msg_size]
                data = data[msg_size:]
                frame = pickle.loads(frame_data)
                text = f"CLIENT: {addr}"
                frame = ps.putBText(frame, text, 10, 10, vspace=10, hspace=1, font_scale=0.7,
                                background_RGB=(255, 0, 0), text_RGB=(255, 250, 250))
                cv2.imshow(f"FROM {addr}", frame)
                key = cv2.waitKey(1) & 0xFF
                if key == ord('q'):
                    break
            client_socket.close()
    except Exception as e:
        print(f"CLINET {addr} DISCONNECTED")
        pass

def run_show_client():
    while True:
        client_socket, addr = server_socket.accept()
        thread = threading.Thread(target=show_client, args=(addr, client_socket))
        thread.start()
        print("TOTAL CLIENTS ", threading.activeCount() - 1)

Screen sharing client:

import socket, cv2, pickle, struct
import imutils
import pyautogui
import numpy as np


def student_screen_show():
    camera = True
    if camera == True:
        vid = cv2.VideoCapture(0)
    else:
        vid = cv2.VideoCapture('videos/mario.mp4')
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host_ip = ' ' 
    port = 9999
    client_socket.connect((host_ip, port))

    resolution = (1920, 1080)
    codec = cv2.VideoWriter_fourcc(*"XVID")
    filename = "Recording.avi"
    fps = 60.0
    out = cv2.VideoWriter(filename, codec, fps, resolution)

    if client_socket:
        while (vid.isOpened()):
            try:
                img = pyautogui.screenshot()
                frame = np.array(img)
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                out.write(frame)
                frame = imutils.resize(frame, width=380)
                a = pickle.dumps(frame)
                message = struct.pack("Q", len(a)) + a
                client_socket.sendall(message)
                cv2.imshow(f"TO: {host_ip}", frame)
                key = cv2.waitKey(1) & 0xFF
                if key == ord("q"):
                    client_socket.close()
            except:
                print('VIDEO FINISHED!')
                break

Webcam sharing client:

import socket, cv2, pickle, struct
import imutils


def student_show():
    camera = True
    if camera == True:
        vid = cv2.VideoCapture(0)
    else:
        vid = cv2.VideoCapture('videos/mario.mp4')
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host_ip = ' ' 

    port = 9999
    client_socket.connect((host_ip, port))
    if client_socket:
    while (vid.isOpened()):
        try:
            img, frame = vid.read()
            frame = imutils.resize(frame, width=380)
            a = pickle.dumps(frame)
            message = struct.pack("Q", len(a)) + a
            client_socket.sendall(message)
            cv2.imshow(f"TO: {host_ip}", frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                client_socket.close()
        except:
            print('VIDEO FINISHED!')
            break

So what I have to do is merge the code so that instead of only running screen sharing or webcam sharing, it shares both at the same time from the same client. While I didn't write the Ip address in the code shown here I am writing my IP address in my code, I just felt weird sharing my Ip address.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source