Best Python code snippet using playwright-python
latency.py
Source:latency.py
1# Copyright 2014 Jetperch LLC - See LICENSE file.2"""3Process touchscreen latency images into finger coordinate and screen coordinate.4The device's touchscreen must be illuminated white so that the finger shows5up black. The device must display a magenta dot at the current touch location. 6"""7import os8import cv29import numpy as np10import scipy.optimize11def _filter_image(img):12 kernel = np.ones((3,3),np.uint8)13 img = cv2.erode(img, kernel)14 return img15def separate_targets(img, threshold=None):16 """Separate the targets from the BGR image.17 18 The finger is mostly black and the touchscreen is magneta.19 20 :param img: The input BGR image.21 :param threshold: The (somewhat arbitrary) pixel value threshold used for22 detection in the range 1 to 255. Default of None is equivalent to 96.23 :return: The tuple of binary images (finger, touchscreen).24 """25 26 img = 255 - img # Invert27 threshold = 96 if threshold is None else threshold28 _, img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)29 # Colors stored as blue, green, red30 # finger is now white and touchscreen is now green31 touchscreen = img[:, :, 1] - img[:, :, 0]32 touchscreen = _filter_image(touchscreen)33 finger = img[:, :, 1] - touchscreen34 finger = _filter_image(finger)35 return (finger, touchscreen)36def finger_image_to_coordinates(img):37 """Find the most vertical point on the finger.38 39 :param img: The finger image.40 :return: The (x, y) point in screen coordinates (int). If the finger 41 cannot be found, return None.42 """43 v = np.max(img, axis=1)44 try:45 y = np.where(v)[0][0]46 x = np.round(np.mean(np.where(img[y])[0]))47 except IndexError:48 return None49 return (int(x), int(y))50def touchscreen_image_to_coordinate(img):51 """Find the center of mass.52 53 :param img: The touchscreen image containing the processed circle.54 :return: The (x, y) point in screen coordinates (int). If the touchscreen55 circle cannot be found, return None.56 """57 # http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#moments58 rv = cv2.moments(img, True)59 if rv['m00'] == 0:60 return None 61 x = rv['m10'] / rv['m00']62 y = rv['m01'] / rv['m00']63 x, y = np.round(x), np.round(y)64 return (int(x), int(y))65def process(img, threshold=None):66 """Process an image into finger and touchscreen coordinates.67 68 :param img: The image to process.69 :param threshold: The threshold for :func:`separate_targets`.70 :return: ((finger_x, finger_y), (touchscreen_x, touchscreeny)) as integer71 coordinates in the image. If either finger or touchscreen is not 72 found, then the corresponding point will be None.73 """74 if np.prod(img.shape) == 0:75 return (None, None)76 finger, touchscreen = separate_targets(img, threshold)77 finger = finger_image_to_coordinates(finger)78 touchscreen = touchscreen_image_to_coordinate(touchscreen)79 return (finger, touchscreen)80def analyze_dir(path, fps):81 """Process and then analyze a directory of png images.82 83 :param path: The path to the directory of images.84 :param fps: The frames per second.85 :return: The latency (seconds) between finger and touchscreen.86 """87 files = [os.path.join(path, o) for o in os.listdir(path) if os.path.isfile(os.path.join(path, o)) and o.endswith('.png')]88 files = sorted(files)89 data = []90 for f in files:91 img = cv2.imread(f)92 data.append(process(img))93 print(data)94 return analyze(data, fps, doPlot=True)95def analyze(data, fps, doPlot=False):96 """Analyze a list of results from :func:`process`.97 98 :param data: The list of results for :func:`process`.99 :param fps: The frames per second.100 :return: The latency (seconds) between finger and touchscreen.101 :raise ValueError: If the data does not contain sufficient information for102 analysis.103 """104 period = 1.0 / float(fps)105 finger_data = []106 touchscreen_data = []107 for idx, (finger, touchscreen) in enumerate(data):108 timestamp = idx * period109 if finger is not None:110 finger_data.append([timestamp, finger[0], finger[1]])111 if touchscreen is not None:112 touchscreen_data.append([timestamp, touchscreen[0], touchscreen[1]])113 finger_data = np.array(finger_data)114 touchscreen_data = np.array(touchscreen_data)115 116 MINIMUM_FRAME_COUNT = 10117 if len(finger_data) < MINIMUM_FRAME_COUNT:118 raise ValueError('finger_data too short')119 if len(touchscreen_data) < MINIMUM_FRAME_COUNT:120 raise ValueError('touchscreen_data too short')121 TIMESTAMP = 0122 X = 1123 Y = 2124 t = touchscreen_data[:, TIMESTAMP]125 126 x0 = [0.0, 0.0, 0.0] # latency in seconds, offset_x, offset_y127 def func(x, details=False):128 latency, offset_x, offset_y = x129 t2 = finger_data[:, TIMESTAMP] + latency130 finger_x = np.interp(t, t2, finger_data[:, X]) - offset_x131 finger_y = np.interp(t, t2, finger_data[:, Y]) - offset_y132 dx = touchscreen_data[:, X] - finger_x133 dy = touchscreen_data[:, Y] - finger_y134 # Discard data that does not overlap135 invalidDataRange = np.logical_or(t < t2[0], t > t2[-1])136 dx[invalidDataRange] = 0.137 dy[invalidDataRange] = 0.138 residuals = np.hstack((dx, dy))139 if details:140 return {'residuals': residuals, 141 'finger_x': finger_x, 'finger_y': finger_y}142 return residuals143 rv = scipy.optimize.leastsq(func, x0)144 x = rv[0]145 if doPlot:146 details = func(x, True)147 import matplotlib.pyplot as plt148 plt.plot(t, details['finger_x'])149 plt.plot(t, touchscreen_data[:, X])150 plt.show()...
touch_screen.py
Source:touch_screen.py
1import logging2import os3import traceback4from threading import Thread5from mopidy import core, exceptions6import pygame7import pykka8from .screen_manager import ScreenManager9logger = logging.getLogger(__name__)10class TouchScreen(pykka.ThreadingActor, core.CoreListener):11 def __init__(self, config, core):12 super(TouchScreen, self).__init__()13 self.core = core14 self.running = False15 self.cursor = config['touchscreen']['cursor']16 self.cache_dir = config['touchscreen']['cache_dir']17 self.fullscreen = config['touchscreen']['fullscreen']18 self.screen_size = (config['touchscreen']['screen_width'],19 config['touchscreen']['screen_height'])20 self.resolution_factor = (config['touchscreen']['resolution_factor'])21 if config['touchscreen']['sdl_fbdev'].lower() != "none":22 os.environ["SDL_FBDEV"] = config['touchscreen']['sdl_fbdev']23 if config['touchscreen']['sdl_mousdrv'].lower() != "none":24 os.environ["SDL_MOUSEDRV"] = (25 config['touchscreen']['sdl_mousdrv'])26 if config['touchscreen']['sdl_mousedev'].lower() != "none":27 os.environ["SDL_MOUSEDEV"] = config['touchscreen']['sdl_mousedev']28 if config['touchscreen']['sdl_audiodriver'].lower() != "none":29 os.environ["SDL_AUDIODRIVER"] = (30 config['touchscreen']['sdl_audiodriver'])31 os.environ["SDL_PATH_DSP"] = config['touchscreen']['sdl_path_dsp']32 pygame.init()33 pygame.display.set_caption("Mopidy-Touchscreen")34 self.get_display_surface(self.screen_size)35 pygame.mouse.set_visible(self.cursor)36 self.screen_manager = ScreenManager(self.screen_size,37 self.core,38 self.cache_dir,39 self.resolution_factor)40 # Raspberry pi GPIO41 self.gpio = config['touchscreen']['gpio']42 if self.gpio:43 from .input import GPIOManager44 pins = {}45 pins['left'] = config['touchscreen']['gpio_left']46 pins['right'] = config['touchscreen']['gpio_right']47 pins['up'] = config['touchscreen']['gpio_up']48 pins['down'] = config['touchscreen']['gpio_down']49 pins['enter'] = config['touchscreen']['gpio_enter']50 self.gpio_manager = GPIOManager(pins)51 def get_display_surface(self, size):52 try:53 if self.fullscreen:54 self.screen = pygame.display.set_mode(55 size, pygame.FULLSCREEN)56 else:57 self.screen = pygame.display.set_mode(size, pygame.RESIZABLE)58 except Exception:59 raise exceptions.FrontendError("Error on display init:\n"60 + traceback.format_exc())61 def start_thread(self):62 clock = pygame.time.Clock()63 pygame.event.set_blocked(pygame.MOUSEMOTION)64 while self.running:65 clock.tick(12)66 self.screen_manager.update(self.screen)67 for event in pygame.event.get():68 if event.type == pygame.QUIT:69 os.system("pkill mopidy")70 elif event.type == pygame.VIDEORESIZE:71 self.get_display_surface(event.size)72 self.screen_manager.resize(event)73 else:74 self.screen_manager.event(event)75 pygame.quit()76 def on_start(self):77 try:78 self.running = True79 thread = Thread(target=self.start_thread)80 thread.start()81 except:82 traceback.print_exc()83 def on_stop(self):84 self.running = False85 def track_playback_started(self, tl_track):86 try:87 self.screen_manager.track_started(tl_track)88 except:89 traceback.print_exc()90 def volume_changed(self, volume):91 self.screen_manager.volume_changed(volume)92 def playback_state_changed(self, old_state, new_state):93 self.screen_manager.playback_state_changed(old_state,94 new_state)95 def tracklist_changed(self):96 try:97 self.screen_manager.tracklist_changed()98 except:99 traceback.print_exc()100 def track_playback_ended(self, tl_track, time_position):101 self.screen_manager.track_playback_ended(tl_track,102 time_position)103 def options_changed(self):104 try:105 self.screen_manager.options_changed()106 except:107 traceback.print_exc()108 def playlists_loaded(self):...
touchscreen_definition.py
Source:touchscreen_definition.py
1# Leviton Cloud Services API model TouchscreenDefinition.2# Auto-generated by api_scraper.py.3#4# Copyright 2017 Tim Lyakhovetskiy <tlyakhov@gmail.com>5#6# This code is released under the terms of the MIT license. See the LICENSE7# file for more details.8from decora_wifi.base_model import BaseModel9class TouchscreenDefinition(BaseModel):10 def __init__(self, session, model_id=None):11 super(TouchscreenDefinition, self).__init__(session, model_id)12 @classmethod13 def count(cls, session, attribs=None):14 if attribs is None:15 attribs = {}16 api = "/TouchscreenDefinitions/count"17 return session.call_api(api, attribs, 'get')18 @classmethod19 def create(cls, session, attribs=None):20 if attribs is None:21 attribs = {}22 api = "/TouchscreenDefinitions"23 return session.call_api(api, attribs, 'post')24 @classmethod25 def create_many(cls, session, attribs=None):26 if attribs is None:27 attribs = {}28 api = "/TouchscreenDefinitions"29 return session.call_api(api, attribs, 'post')30 def delete_by_id(self, attribs=None):31 if attribs is None:32 attribs = {}33 api = "/TouchscreenDefinitions/{0}".format(self._id)34 return self._session.call_api(api, attribs, 'delete')35 def exists(self, attribs=None):36 if attribs is None:37 attribs = {}38 api = "/TouchscreenDefinitions/{0}/exists".format(self._id)39 return self._session.call_api(api, attribs, 'get')40 @classmethod41 def find(cls, session, attribs=None):42 if attribs is None:43 attribs = {}44 api = "/TouchscreenDefinitions"45 items = session.call_api(api, attribs, 'get')46 result = []47 if items is not None:48 for data in items:49 model = TouchscreenDefinition(session, data['id'])50 model.data = data51 result.append(model)52 return result53 def find_by_id(self, attribs=None):54 if attribs is None:55 attribs = {}56 api = "/TouchscreenDefinitions/{0}".format(self._id)57 data = self._session.call_api(api, attribs, 'get')58 self.data.update(data)59 return self60 @classmethod61 def find_one(cls, session, attribs=None):62 if attribs is None:63 attribs = {}64 api = "/TouchscreenDefinitions/findOne"65 return session.call_api(api, attribs, 'get')66 def refresh(self):67 api = "/TouchscreenDefinitions/{0}".format(self._id)68 result = self._session.call_api(api, {}, 'get')69 if result is not None:70 self.data.update(result)71 return self72 def get_whitelist(self, attribs=None):73 if attribs is None:74 attribs = {}75 api = "/TouchscreenDefinitions/{0}/whitelist".format(self._id)76 data = self._session.call_api(api, attribs, 'get')77 from .whitelist import Whitelist78 model = Whitelist(self._session, data['id'])79 model.data = data80 return model81 def replace_by_id(self, attribs=None):82 if attribs is None:83 attribs = {}84 api = "/TouchscreenDefinitions/{0}/replace".format(self._id)85 return self._session.call_api(api, attribs, 'post')86 @classmethod87 def replace_or_create(cls, session, attribs=None):88 if attribs is None:89 attribs = {}90 api = "/TouchscreenDefinitions/replaceOrCreate"91 return session.call_api(api, attribs, 'post')92 def update_attributes(self, attribs=None):93 if attribs is None:94 attribs = {}95 api = "/TouchscreenDefinitions/{0}".format(self._id)96 data = self._session.call_api(api, attribs, 'put')97 self.data.update(attribs)98 return self99 @classmethod100 def upsert(cls, session, attribs=None):101 if attribs is None:102 attribs = {}103 api = "/TouchscreenDefinitions"104 data = session.call_api(api, attribs, 'put')105 model = TouchscreenDefinition(session, data['id'])106 model.data = data107 return model108 @classmethod109 def upsert_with_where(cls, session, attribs=None):110 if attribs is None:111 attribs = {}112 api = "/TouchscreenDefinitions/upsertWithWhere"...
usesFeature.py
Source:usesFeature.py
1#! /usr/bin/python2import xml.dom.minidom as xdom3from optparse import OptionParser4# option parse5parser = OptionParser(usage="usage: %prog [options]")6parser.add_option("-l","--list",dest="autoGenList",metavar="FEATURELIST",help="get the user feature list")7(options,args) = parser.parse_args()8if len(args) != 0:9 parser.print_help()10try:11 autoList = options.autoGenList.split()12except AttributeError:13 autoList = []14# through the logical we can set the we needed autogen xml list15# TODO:we use the autoList to save the xml list16# eg, if contidion:17# autoList.append(xml)18# ...19####################################################20# uses-feature xml mapping table 21####################################################22xmlHash = {};23xmlHash["android.hardware.bluetooth"] = ["android.hardware.bluetooth"]24xmlHash["android.hardware.camera"] = ["android.hardware.camera","android.hardware.camera.autofocus"]25xmlHash["android.hardware.camera.autofocus"] = ["android.hardware.camera","android.hardware.camera.autofocus"]26xmlHash["android.hardware.location"] = ["android.hardware.location","android.hardware.location.network"]27xmlHash["android.hardware.location.gps"] = ["android.hardware.location","android.hardware.location.network","android.hardware.location.gps"]28#xmlHash["android.hardware.location.network"] = []29xmlHash["android.hardware.microphone"] = ["android.hardware.microphone"]30xmlHash["android.hardware.sensor.compass"] = ["android.hardware.sensor.compass"]31xmlHash["android.hardware.sensor.accelerometer"] = ["android.hardware.sensor.accelerometer"]32xmlHash["android.hardware.sensor.light"] = ["android.hardware.sensor.light"]33xmlHash["android.hardware.sensor.gyroscope"] = ["android.hardware.sensor.gyroscope"]34xmlHash["android.hardware.sensor.barometer"] = ["android.hardware.sensor.barometer"]35xmlHash["android.hardware.sensor.proximity"] = ["android.hardware.sensor.proximity"]36#xmlHash["android.hardware.telephony"]37xmlHash["android.hardware.telephony.cdma"] = ["android.hardware.telephony","android.hardware.telephony.cdma"]38xmlHash["android.hardware.telephony.gsm"] = ["android.hardware.telephony","android.hardware.telephony.gsm"]39xmlHash["android.hardware.touchscreen"] = ["android.hardware.touchscreen"]40xmlHash["android.hardware.touchscreen.multitouch"] = ["android.hardware.touchscreen","android.hardware.touchscreen.multitouch"]41xmlHash["android.hardware.touchscreen.multitouch.distinct"] = ["android.hardware.touchscreen","android.hardware.touchscreen.multitouch","android.hardware.touchscreen.multitouch.distinct"]42xmlHash["android.hardware.touchscreen.multitouch.jazzhand"] = ["android.hardware.touchscreen","android.hardware.touchscreen.multitouch","android.hardware.touchscreen.multitouch.distinct","android.hardware.touchscreen.multitouch.jazzhand"]43xmlHash["android.software.live_wallpaper"] = ["android.software.live_wallpaper"]44xmlHash["android.hardware.wifi"] = ["android.hardware.wifi"]45xmlHash["android.software.sip"] = ["android.software.sip"]46xmlHash["android.software.sip.voip"] = ["android.software.sip","android.software.sip.voip"]47####################################################48def autoGen(xmlName,xmlContent):49 # create document50 doc = xdom.Document()51 # create permissions node52 permissions = doc.createElement("permissions")53 doc.appendChild(permissions)54 # create feature node55 for content in xmlContent:56 feature = doc.createElement("feature")57 feature.setAttribute("name",content)58 permissions.appendChild(feature)59 xmlfile = open("%s.xml"%xmlName,"w")60 xmlfile.write(doc.toprettyxml(indent = " ",encoding="utf-8"))61 xmlfile.close()62for name in autoList:...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!