From 5853ae6dda4477e3cffaa9f3129e8e60af232b72 Mon Sep 17 00:00:00 2001 From: Petr Kracik Date: Thu, 4 Jun 2020 13:50:01 +0200 Subject: [PATCH] Code style --- hidinput.py | 19 +++---- jskeycodes.py | 131 ------------------------------------------------ webstreaming.py | 109 ++++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 195 deletions(-) diff --git a/hidinput.py b/hidinput.py index 1f175df..d3aab59 100644 --- a/hidinput.py +++ b/hidinput.py @@ -4,8 +4,6 @@ from flask import render_template from flask import request import threading import argparse -import datetime -import time import json from hidkeycodes import hidkeycodes @@ -20,10 +18,12 @@ app = Flask(__name__) hiddev = None hidmouse = None + def hid_init(): global hiddev, hidmouse - hiddev=open('/dev/hidg0', 'rb+') - hidmouse=open('/dev/hidg1', 'rb+') + hiddev = open('/dev/hidg0', 'rb+') + hidmouse = open('/dev/hidg1', 'rb+') + def hid_write(data): if hiddev is None: @@ -32,6 +32,7 @@ def hid_write(data): hiddev.write(data) hiddev.flush() + def hid_mouse_write(btn, x, y, wheel): if hidmouse is None: return False @@ -57,7 +58,7 @@ def send_key(hidkey, shift, alt, ctlr, mod): if mod and (hidkey == hidkeycodes['KEY_MOD_LMETA'] or hidkey == hidkeycodes['KEY_MOD_RMETA']): data[0] += hidkey - hidkey=0 + hidkey = 0 data[2] = hidkey @@ -104,17 +105,17 @@ def mouse(): x = x if x >= 0 else 255-abs(x) y = y if y >= 0 else 255-abs(y) - print ("X: {}, Y: {}".format(x,y)) + print("X: {}, Y: {}".format(x, y)) hid_mouse_write(btn, x, y, wheel) - return Response("", mimetype = "text/plain") + return Response("", mimetype="text/plain") @app.route("/hid/keyboard", methods=["POST"]) def keypress(): keyevent = json.loads(request.data) - print ("Raw data: {}".format(request.data)) + print("Raw data: {}".format(request.data)) rawkeycode = keyevent['code'] hidkeycode, mod = get_hid_by_jscode(rawkeycode) @@ -130,7 +131,7 @@ def keypress(): except Exception as e: print("Error sending HID message", e) - return Response("Press {}".format(hidkeycode), mimetype = "text/plain") + return Response("Press {}".format(hidkeycode), mimetype="text/plain") # check to see if this is the main thread of execution diff --git a/jskeycodes.py b/jskeycodes.py index ac00edd..d73418f 100644 --- a/jskeycodes.py +++ b/jskeycodes.py @@ -1,134 +1,3 @@ - -jskeycodes = { - 'KEY_A': 'KeyA', - 'KEY_B': 'KeyB', - 'KEY_C': 'KeyC', - 'KEY_D': 'KeyD', - 'KEY_E': 'KeyE', - 'KEY_F': 'KeyF', - 'KEY_G': 'KeyG', - 'KEY_H': 'KeyH', - 'KEY_I': 'KeyI', - 'KEY_J': 'KeyJ', - 'KEY_K': 'KeyK', - 'KEY_L': 'KeyL', - 'KEY_M': 'KeyM', - 'KEY_N': 'KeyN', - 'KEY_O': 'KeyO', - 'KEY_P': 'KeyP', - 'KEY_Q': 'KeyQ', - 'KEY_R': 'KeyR', - 'KEY_S': 'KeyS', - 'KEY_T': 'KeyT', - 'KEY_U': 'KeyU', - 'KEY_V': 'KeyV', - 'KEY_W': 'KeyW', - 'KEY_X': 'KeyX', - 'KEY_Y': 'KeyY', - 'KEY_Z': 'KeyZ', - - 'KEY_GRAVE': 'Backquote', - - 'KEY_0': 'Digit0', - 'KEY_1': 'Digit1', - 'KEY_2': 'Digit2', - 'KEY_3': 'Digit3', - 'KEY_4': 'Digit4', - 'KEY_5': 'Digit5', - 'KEY_6': 'Digit6', - 'KEY_7': 'Digit7', - 'KEY_8': 'Digit8', - 'KEY_9': 'Digit9', - - 'KEY_MINUS': 'Minus', - 'KEY_EQUAL': 'Equal', - - 'KEY_LEFTBRACE': 'BracketLeft', - 'KEY_RIGHTBRACE': 'BracketRight', - - 'KEY_SEMICOLON': 'Semicolon', - 'KEY_APOSTROPHE': 'Quote', - 'KEY_BACKSLASH': 'Backslash', - - 'KEY_COMMA': 'Comma', - 'KEY_DOT': 'Period', - 'KEY_SLASH': 'Slash', - - 'KEY_102ND': 'IntlBackslash', - - 'KEY_KP0': 'Numpad0', - 'KEY_KP1': 'Numpad1', - 'KEY_KP2': 'Numpad2', - 'KEY_KP3': 'Numpad3', - 'KEY_KP4': 'Numpad4', - 'KEY_KP5': 'Numpad5', - 'KEY_KP6': 'Numpad6', - 'KEY_KP7': 'Numpad7', - 'KEY_KP8': 'Numpad8', - 'KEY_KP9': 'Numpad9', - 'KEY_KPASTERISK': 'NumpadMultiply', - 'KEY_KPPLUS': 'NumpadAdd', - 'KEY_KPMINUS': 'NumpadSubtract', - 'KEY_KPDOT': 'NumpadDecimal', - 'KEY_KPSLASH': 'NumpadDivide', - 'KEY_KPENTER': 'NumpadEnter', - - 'KEY_ESC': 'Escape', - 'KEY_F1': 'F1', - 'KEY_F2': 'F2', - 'KEY_F3': 'F3', - 'KEY_F4': 'F4', - 'KEY_F5': 'F5', - 'KEY_F6': 'F6', - 'KEY_F7': 'F7', - 'KEY_F8': 'F8', - 'KEY_F9': 'F9', - 'KEY_F10': 'F10', - 'KEY_F11': 'F11', - 'KEY_F12': 'F12', - 'KEY_F13': 'F13', - 'KEY_F14': 'F14', - 'KEY_F15': 'F15', - 'KEY_F16': 'F16', - 'KEY_F17': 'F17', - 'KEY_F18': 'F18', - 'KEY_F19': 'F19', - 'KEY_F20': 'F20', - 'KEY_F21': 'F21', - 'KEY_F22': 'F22', - 'KEY_F23': 'F23', - - 'KEY_BACKSPACE': 'Backspace', - 'KEY_SPACE': 'Space', - 'KEY_TAB': 'Tab', - 'KEY_ENTER' : 'Enter', - - 'KEY_PAGEUP': 'PageUp', - 'KEY_PAGEDOWN': 'PageDown', - 'KEY_HOME': 'Home', - 'KEY_END': 'End', - 'KEY_INSERT': 'Insert', - 'KEY_DELETE': 'Delete', - - 'KEY_UP': 'ArrowUp', - 'KEY_DOWN': 'ArrowDown', - 'KEY_LEFT': 'ArrowLeft', - 'KEY_RIGHT': 'ArrowRight', - - 'KEY_NUMLOCK': 'NumLock', - 'KEY_CAPSLOCK': 'CapsLock', - 'KEY_SCROLLLOCK': 'ScrollLock', - - 'KEY_MOD_LMETA': 'MetaLeft', - 'KEY_MOD_RMETA': 'MetaRight', - -# Because firefox must sending different - 'KEY_MOD_LMETA': 'OSLeft', - 'KEY_MOD_RMETA': 'OSRight', - - 'KEY_COMPOSE': 'ContextMenu', -} - jscodehidmap = { 'KeyA': 'KEY_A', 'KeyB': 'KEY_B', diff --git a/webstreaming.py b/webstreaming.py index 4f77ee5..cc39796 100644 --- a/webstreaming.py +++ b/webstreaming.py @@ -4,7 +4,6 @@ from flask import render_template import threading import argparse import datetime -import imutils import time import cv2 from hashlib import md5 @@ -27,77 +26,77 @@ time.sleep(1.0) @app.route("/") def index(): - # return the rendered template - return render_template("index.html") + # return the rendered template + return render_template("index.html") def detect_motion(frameCount): - global vs, outputFrame, lock - lastframe = None - while True: - flag, frame = cap.read() - framedig = md5(frame).digest() + global vs, outputFrame, lock + lastframe = None + while True: + flag, frame = cap.read() + framedig = md5(frame).digest() - if lastframe == framedig: - print("Duplicate frame, skipping") - continue + if lastframe == framedig: + print("Duplicate frame, skipping") + continue - if not flag: - continue - lastframe = framedig + if not flag: + continue + lastframe = framedig - timestamp = datetime.datetime.now() - cv2.putText(frame, timestamp.strftime( - "%A %d %B %Y %I:%M:%S%p"), (10, 15), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1) + timestamp = datetime.datetime.now() + cv2.putText(frame, timestamp.strftime( + "%A %d %B %Y %I:%M:%S%p"), (10, 15), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1) + + with lock: + outputFrame = frame.copy() - with lock: - outputFrame = frame.copy() def generate(): - global outputFrame, lock - while True: - with lock: - if outputFrame is None: - continue + global outputFrame, lock + while True: + with lock: + if outputFrame is None: + continue - (flag, encodedImage) = cv2.imencode(".jpg", outputFrame) - # ensure the frame was successfully encoded - if not flag: - continue + (flag, encodedImage) = cv2.imencode(".jpg", outputFrame) + # ensure the frame was successfully encoded + if not flag: + continue - # yield the output frame in the byte format - yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + - bytearray(encodedImage) + b'\r\n') + # yield the output frame in the byte format + yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + + bytearray(encodedImage) + b'\r\n') - time.sleep(0.2) + time.sleep(0.2) @app.route("/video_feed") def video_feed(): - # return the response generated along with the specific media - # type (mime type) - return Response(generate(), - mimetype = "multipart/x-mixed-replace; boundary=frame") + # return the response generated along with the specific media + # type (mime type) + return Response(generate(), + mimetype = "multipart/x-mixed-replace; boundary=frame") # check to see if this is the main thread of execution if __name__ == '__main__': - # construct the argument parser and parse command line arguments - ap = argparse.ArgumentParser() - ap.add_argument("-i", "--ip", type=str, required=True, - help="ip address of the device") - ap.add_argument("-o", "--port", type=int, required=True, - help="ephemeral port number of the server (1024 to 65535)") - ap.add_argument("-f", "--frame-count", type=int, default=32, - help="# of frames used to construct the background model") - args = vars(ap.parse_args()) - # start a thread that will perform motion detection - t = threading.Thread(target=detect_motion, args=( - args["frame_count"],)) - t.daemon = True - t.start() - # start the flask app - app.run(host=args["ip"], port=args["port"], debug=True, - threaded=True, use_reloader=False) - + # construct the argument parser and parse command line arguments + ap = argparse.ArgumentParser() + ap.add_argument("-i", "--ip", type=str, required=True, + help="ip address of the device") + ap.add_argument("-o", "--port", type=int, required=True, + help="ephemeral port number of the server (1024 to 65535)") + ap.add_argument("-f", "--frame-count", type=int, default=32, + help="# of frames used to construct the background model") + args = vars(ap.parse_args()) + # start a thread that will perform motion detection + t = threading.Thread(target=detect_motion, args=( + args["frame_count"],)) + t.daemon = True + t.start() + # start the flask app + app.run(host=args["ip"], port=args["port"], debug=True, + threaded=True, use_reloader=False)