Best Python code snippet using Airtest
dataset_data_provider_test.py
Source:dataset_data_provider_test.py
...30from tensorflow.python.ops import io_ops31from tensorflow.python.ops import parsing_ops32from tensorflow.python.platform import gfile33from tensorflow.python.platform import test34def _resize_image(image, height, width):35 image = array_ops.expand_dims(image, 0)36 image = image_ops.resize_bilinear(image, [height, width])37 return array_ops.squeeze(image, [0])38def _create_tfrecord_dataset(tmpdir):39 if not gfile.Exists(tmpdir):40 gfile.MakeDirs(tmpdir)41 data_sources = test_utils.create_tfrecord_files(tmpdir, num_files=1)42 keys_to_features = {43 'image/encoded':44 parsing_ops.FixedLenFeature(45 shape=(), dtype=dtypes.string, default_value=''),46 'image/format':47 parsing_ops.FixedLenFeature(48 shape=(), dtype=dtypes.string, default_value='jpeg'),49 'image/class/label':50 parsing_ops.FixedLenFeature(51 shape=[1],52 dtype=dtypes.int64,53 default_value=array_ops.zeros(54 [1], dtype=dtypes.int64))55 }56 items_to_handlers = {57 'image': tfexample_decoder.Image(),58 'label': tfexample_decoder.Tensor('image/class/label'),59 }60 decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,61 items_to_handlers)62 return dataset.Dataset(63 data_sources=data_sources,64 reader=io_ops.TFRecordReader,65 decoder=decoder,66 num_samples=100,67 items_to_descriptions=None)68class DatasetDataProviderTest(test.TestCase):69 def testTFRecordDataset(self):70 dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),71 'tfrecord_dataset'))72 height = 30073 width = 28074 with self.test_session():75 test_dataset = _create_tfrecord_dataset(dataset_dir)76 provider = dataset_data_provider.DatasetDataProvider(test_dataset)77 key, image, label = provider.get(['record_key', 'image', 'label'])78 image = _resize_image(image, height, width)79 with session.Session('') as sess:80 with queues.QueueRunners(sess):81 key, image, label = sess.run([key, image, label])82 split_key = key.decode('utf-8').split(':')83 self.assertEqual(2, len(split_key))84 self.assertEqual(test_dataset.data_sources[0], split_key[0])85 self.assertTrue(split_key[1].isdigit())86 self.assertListEqual([height, width, 3], list(image.shape))87 self.assertListEqual([1], list(label.shape))88 def testTFRecordSeparateGetDataset(self):89 dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),90 'tfrecord_separate_get'))91 height = 30092 width = 28093 with self.test_session():94 provider = dataset_data_provider.DatasetDataProvider(95 _create_tfrecord_dataset(dataset_dir))96 [image] = provider.get(['image'])97 [label] = provider.get(['label'])98 image = _resize_image(image, height, width)99 with session.Session('') as sess:100 with queues.QueueRunners(sess):101 image, label = sess.run([image, label])102 self.assertListEqual([height, width, 3], list(image.shape))103 self.assertListEqual([1], list(label.shape))104 def testConflictingRecordKeyItem(self):105 dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(),106 'tfrecord_dataset'))107 with self.test_session():108 with self.assertRaises(ValueError):109 dataset_data_provider.DatasetDataProvider(110 _create_tfrecord_dataset(dataset_dir), record_key='image')111if __name__ == '__main__':112 test.main()
VideoPanel.py
Source:VideoPanel.py
1from queue import Queue2from threading import Lock3from typing import Optional4from PySide2.QtCore import Qt, Signal, Slot, QThread, QSize, QCoreApplication5from PySide2.QtGui import QResizeEvent, QImage, QPainter, QPaintEvent, QPixmap6from PySide2.QtWidgets import QSizePolicy, QWidget, QStyleOption7from numpy import ndarray8from livia_ui.gui.views.utils import convert_image_opencv_to_qt9class _ImageProcessingThread(QThread):10 update_image_signal: Signal = Signal(QPixmap)11 clear_image_signal: Signal = Signal()12 def __init__(self, resize_image: bool, size: QSize):13 super().__init__()14 self._queue: Queue = Queue()15 self._resize_image: bool = resize_image16 self._size: QSize = size17 self._last_image: Optional[QImage] = None18 self._running: bool = False19 self._lock: Lock = Lock()20 def run(self) -> None:21 self._running = True22 while self._running:23 image = self._queue.get(True)24 with self._lock:25 if isinstance(image, ndarray):26 image = convert_image_opencv_to_qt(image)27 if isinstance(image, QImage):28 self._last_image = image29 i_size = self._last_image.size()30 if self._resize_image and self._size != i_size:31 resized_image = self._last_image.scaled(self._size.width(), self._size.height(),32 Qt.KeepAspectRatio, Qt.SmoothTransformation)33 else:34 resized_image = self._last_image35 self.update_image_signal.emit(QPixmap.fromImage(resized_image))36 else:37 self._last_image = None38 self.clear_image_signal.emit()39 def stop(self):40 self._running = False41 self._queue.put(None)42 def is_image_resizable(self) -> bool:43 return self._resize_image44 def set_image_size(self, size: QSize):45 if self._size != size:46 with self._lock:47 if self._size != size:48 self._size = size49 self._refresh_image()50 def set_image_resizable(self, resizable: bool):51 if self._resize_image != resizable:52 with self._lock:53 if self._resize_image != resizable:54 self._resize_image = resizable55 self._refresh_image()56 def clear_image(self):57 if self._last_image is not None:58 if self._lock:59 if self._last_image is not None:60 self._last_image = None61 self._refresh_image()62 def add_image(self, image: Optional[ndarray]):63 self._queue.put(image)64 def _refresh_image(self):65 self._queue.put(self._last_image)66class VideoPanel(QWidget):67 def __init__(self, resize_image: bool = True, *args, **kwargs):68 super(VideoPanel, self).__init__(*args, **kwargs)69 self._painter: QPainter = QPainter()70 self._image: Optional[QPixmap] = None71 self._resize_image: bool = resize_image72 self._no_image_text: str = QCoreApplication.translate(self.__class__.__name__, "No image")73 self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)74 self._thread: _ImageProcessingThread = _ImageProcessingThread(resize_image, self.size())75 self._thread.start()76 self._thread.setPriority(QThread.HighPriority)77 self.moveToThread(self._thread)78 self._thread.update_image_signal.connect(self._on_update_image_signal)79 self._thread.clear_image_signal.connect(self._on_clear_image_signal)80 def paintEvent(self, event: QPaintEvent):81 rect = event.rect()82 event.accept()83 options: QStyleOption = QStyleOption()84 options.initFrom(self)85 self._painter.begin(self)86 self._painter.fillRect(rect, Qt.black)87 if self._image:88 image_rect = self._image.rect()89 image_rect.moveCenter(rect.center())90 self._painter.drawPixmap(image_rect, self._image)91 else:92 self._painter.setPen(Qt.white)93 self._painter.drawText(rect.center(), self._no_image_text)94 self._painter.end()95 @Slot(QPixmap)96 def _on_update_image_signal(self, image: QImage):97 self._image = image98 self.repaint()99 @Slot()100 def _on_clear_image_signal(self):101 self._image = None102 self.repaint()103 def resizeEvent(self, event: QResizeEvent):104 self._thread.set_image_size(event.size())105 def is_image_resizable(self) -> bool:106 return self._thread.is_image_resizable()107 def set_image_resizable(self, resizable: bool):108 self._thread.set_image_resizable(resizable)109 def show_frame(self, frame: Optional[ndarray]):110 self._thread.add_image(frame)111 def clear_frame(self):...
ImagePanel.py
Source:ImagePanel.py
1from typing import Optional2from PySide2.QtCore import Qt, QCoreApplication, Signal, Slot3from PySide2.QtGui import QResizeEvent, QImage, QPixmap4from PySide2.QtWidgets import QLabel, QSizePolicy5from numpy import ndarray6from livia_ui.gui.views.utils import convert_image_opencv_to_qt7class ImagePanel(QLabel):8 _update_image_signal: Signal = Signal(QPixmap)9 _clear_image_signal: Signal = Signal()10 def __init__(self, resize_image: bool = True, *args, **kwargs):11 super(ImagePanel, self).__init__(*args, **kwargs)12 self._resize_image: bool = resize_image13 self._last_image: Optional[QImage] = None14 self._processing_text = QCoreApplication.translate(self.__class__.__name__, "Processing image...")15 self.setMinimumSize(800, 600)16 self.setAlignment(Qt.AlignCenter)17 self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)18 self.setAutoFillBackground(True)19 self.setText(self._processing_text)20 self._update_image_signal.connect(self._on_update_image_signal)21 self._clear_image_signal.connect(self._on_clear_image_signal)22 @Slot(QPixmap)23 def _on_update_image_signal(self, image: QPixmap):24 self.setPixmap(image)25 @Slot()26 def _on_clear_image_signal(self):27 self.setText(self._processing_text)28 def resizeEvent(self, event: QResizeEvent):29 if self._resize_image:30 self.refresh_image()31 def is_image_resizable(self) -> bool:32 return self._resize_image33 def set_image_resizable(self, resizable: bool):34 if self._resize_image != resizable:35 self._resize_image = resizable36 self.refresh_image()37 def show_frame(self, frame: Optional[ndarray]):38 if frame is not None:39 self._last_image = convert_image_opencv_to_qt(frame)40 self._display_image(self._last_image)41 else:42 self._clear_image_signal.emit()43 def refresh_image(self):44 if self._last_image is not None:45 self._display_image(self._last_image)46 def clear_frame(self):47 self._clear_image_signal.emit()48 def _display_image(self, image: QImage):49 size = self.size()50 if self._resize_image and image.size() != size:51 image = image.scaled(size.width(), size.height(), Qt.KeepAspectRatio)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!