Last active
April 25, 2020 13:59
-
-
Save gbhgit/c90f3170f576eaf26179e2cf88ab5429 to your computer and use it in GitHub Desktop.
Socket Client-Sever Images Transfer
This file contains 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 os | |
import cv2 | |
import time | |
import base64 | |
import socketio | |
import threading | |
from datetime import datetime | |
# all images .jpg in folder and subfolders of 'path_to_send_images' generates | |
# a connection to upload images via socket for corresponding path in client machine | |
path_to_send_images = "/var/www/html/" | |
threads_path = [] | |
def run_app_path(idx): | |
# Start socket client | |
client_socket = "http://IP:PORT" | |
sio = socketio.Client() | |
sio.connect(client_socket) | |
image_path = threads_path[idx] | |
while(True): | |
if (sio.connected): | |
image = cv2.imread(image_path) | |
retval, buffer = cv2.imencode('.jpg', image) | |
imgbase64 = base64.b64encode(buffer).decode("utf-8") | |
message = {'frame': imgbase64, 'path2save': image_path} | |
sio.emit('save_frame', message) | |
print('Client Socket: Sent to server') | |
else: | |
print("Error, Socket Not Connected of path:", image_path) | |
time.sleep(0.3) | |
if __name__ == '__main__': | |
for root, dirs, files in os.walk(path_to_send_images): | |
for file in files: | |
if file.endswith(".jpg"): | |
threads_path.append( os.path.join(root, file) ) | |
idx = 0 | |
for _path in threads_path: | |
try: | |
x = threading.Thread(target=run_app_path, args=(idx, ) ) | |
x.start() | |
except: | |
print("Error: unable to start thread") | |
idx = idx + 1 |
This file contains 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 time | |
import base64 | |
import eventlet | |
import socketio | |
import numpy as np | |
from datetime import datetime | |
sio = socketio.Server() | |
app = socketio.WSGIApp(sio) | |
starting = False | |
ClientsOn = [] | |
@sio.event | |
def connect(sid, environ): | |
ClientsOn.append(sid) | |
#sio.start_background_task(worker, sio, sid) | |
print('[PYTHON]: Server Socket ', sid, ' connected!') | |
@sio.event | |
def save_frame(sid, data): | |
#ClientsOn.append(sid) | |
#sio.start_background_task(worker, sio, sid) | |
base64_data = data["frame"] | |
filename = data["path2save"] | |
print('[PYTHON]: Server Socket ', sid, ' received!') | |
#print('[PYTHON]: Server Socket ', base64_data) | |
imgdata = base64.b64decode(base64_data) | |
with open(filename, 'wb') as f: | |
f.write(imgdata) | |
@sio.event | |
def disconnect(sid): | |
ClientsOn.remove(sid) | |
print('[PYTHON]: Server Socket ', sid, ' disconneted!') | |
if __name__ == '__main__': | |
#from gevent import monkey | |
#monkey.patch_all() | |
eventlet.wsgi.server(eventlet.listen(('IP', PORT)), app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment