Move parts of backends into new imgproc.py

A lot of code was unnecesarily shared between the backends. This places
much of that code in a new file, `imgproc.py`
This commit is contained in:
Samuel Sloniker 2021-06-17 19:50:16 -07:00
parent d96f6efc74
commit a67f765bd2
5 changed files with 65 additions and 72 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
__pycache__
server/pieces
server/newlist
server/oldlist

View File

@ -1,48 +1,14 @@
import os
import requests
import threading
import json
def get_img():
raw_img = requests.get('http://localhost:8080/get_capture.bmp').content
with open('full_img.bmp', 'wb+') as f:
with open('img.bmp', 'wb+') as f:
f.write(raw_img)
os.system('convert full_img.bmp -resize 800x480 img.bmp')
with open('crops.json') as f:
crops = json.load(f)
return 'img.bmp'
threads = []
for crop in crops:
threads.append(threading.Thread(target=os.system, args=(f'convert img.bmp -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',)))
threads[-1].start()
for thread in threads:
thread.join()
os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist")
changed = []
with open('newlist') as f:
lines = f.readlines()
for line in lines:
changed.append(line.split(": ")[0].split('.')[0].split('/')[1])
os.system("md5sum pieces/* > oldlist")
return changed
def get_full_img():
raw_img = requests.get('http://localhost:8080/get_capture.bmp').content
with open('full_full.bmp', 'wb+') as f:
f.write(raw_img)
os.system('convert full_full.bmp -resize 800x480 full.jpg')
os.unlink('full_full.bmp')
return 'full.jpg'
def touch(x, y, w, is_long):
x = round(800 * x / w)
y = round(800 * y / w)
def touch(x, y, is_long):
requests.get(f'http://localhost:8080/set_touch?x={x}&y={y}&hold={1 if is_long else 0}')

View File

@ -1,41 +1,21 @@
import os
import requests
import threading
import json
import time
def _long_touch(x, y):
os.system(f'xdotool mousemove {x} {y} mousedown 1')
time.sleep(2)
os.system(f'xdotool mouseup 1')
def get_img():
os.system('xwd -root -silent | convert xwd:- img.bmp')
with open('crops.json') as f:
crops = json.load(f)
return 'img.bmp'
threads = []
for crop in crops:
threads.append(threading.Thread(target=os.system, args=(f'convert img.bmp -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',)))
threads[-1].start()
for thread in threads:
thread.join()
os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist")
changed = []
with open('newlist') as f:
lines = f.readlines()
for line in lines:
changed.append(line.split(": ")[0].split('.')[0].split('/')[1])
os.system("md5sum pieces/* > oldlist")
return changed
def get_full_img():
os.system('xwd -root -silent | convert xwd:- full.jpg')
return 'full.jpg'
def touch(x, y, w, is_long):
x = round(800 * x / w)
y = round(800 * y / w)
def touch(x, y, is_long):
if is_long:
os.system(f'xdotool mousemove {x} {y} mousedown 1')
time.sleep(2)
os.system(f'xdotool mouseup 1')
threading.Thread(target=_long_touch, args=(x, y,)).start()
else:
os.system(f'xdotool mousemove {x} {y} click 1')

42
server/imgproc.py Normal file
View File

@ -0,0 +1,42 @@
import json
import threading
import os
def get_split_imgs():
bmp_path = backend.get_img()
with open('crops.json') as f:
crops = json.load(f)
threads = []
for crop in crops:
threads.append(threading.Thread(target=os.system, args=(f'convert {bmp_path} -crop {crop} pieces/{crop.split("+", 1)[1].replace("+", "x")}.jpg',)))
threads[-1].start()
for thread in threads:
thread.join()
os.unlink(bmp_path)
os.system("md5sum -c oldlist 2>/dev/null | grep FAILED > newlist")
changed = []
with open('newlist') as f:
lines = f.readlines()
for line in lines:
changed.append(line.split(": ")[0].split('.')[0].split('/')[1])
os.system("md5sum pieces/* > oldlist")
return changed
def get_full_img():
bmp_path = backend.get_img()
os.system(f'convert {bmp_path} img.jpg')
os.unlink(bmp_path)
return 'img.jpg'
def touch(x, y, w, is_long):
x = round(800 * x / w)
y = round(800 * y / w)
backend.touch(x, y, is_long)

View File

@ -7,19 +7,21 @@ import time
import shutil
import tempfile
from websocket_server import WebsocketServer
import imgproc as hcapi
# Select backend - backends.port8080 uses HamClock's port 8080 service;
# backends.x11 uses an X11 server (typically Xvfb) (make sure DISPLAY is set
# correctly!)
#import backends.port8080 as hcapi
import backends.x11 as hcapi
#import backends.port8080 as backend
import backends.x11 as backend
hcapi.backend = backend
def cycle():
try:
changed = hcapi.get_img()
changed = hcapi.get_split_imgs()
except Exception as e:
for client in clients.values():
client.send('err%noconn%Server failed to capture screenshot', 'ERR')