|
|
@@ -1,7 +1,10 @@
|
|
|
import cv2
|
|
|
+import datetime
|
|
|
+import io
|
|
|
import numpy as np
|
|
|
import os
|
|
|
import threading
|
|
|
+import subprocess
|
|
|
from io import BytesIO
|
|
|
from PIL import Image
|
|
|
from ppadb.client import Client as AdbClient
|
|
|
@@ -23,9 +26,19 @@ def get_current_screen():
|
|
|
return current_screen
|
|
|
|
|
|
|
|
|
-def capture_current_screen():
|
|
|
- global current_screen
|
|
|
- current_screen = device.screencap()
|
|
|
+def capture_current_screen(timeout=5): # Timeout in seconds
|
|
|
+ def target():
|
|
|
+ global current_screen
|
|
|
+ current_screen = device.screencap()
|
|
|
+
|
|
|
+ capture_thread = threading.Thread(target=target)
|
|
|
+ capture_thread.start()
|
|
|
+ capture_thread.join(timeout)
|
|
|
+
|
|
|
+ if capture_thread.is_alive():
|
|
|
+ print("Screen capture timed out")
|
|
|
+ # Handle the timeout situation, e.g., by retrying or aborting
|
|
|
+ capture_thread.join()
|
|
|
|
|
|
return current_screen
|
|
|
|
|
|
@@ -203,6 +216,39 @@ def non_max_suppression(boxes, overlapThresh):
|
|
|
return boxes[pick].astype("int")
|
|
|
|
|
|
|
|
|
+def save_screenshot(path="test"):
|
|
|
+ # Take a screenshot
|
|
|
+ result = capture_current_screen()
|
|
|
+
|
|
|
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
+ image = Image.open(io.BytesIO(result))
|
|
|
+ jpeg_filename = f"{path}/{timestamp}.jpg"
|
|
|
+
|
|
|
+ image = image.convert("RGB") # Convert to RGB mode for JPEG
|
|
|
+ with open(jpeg_filename, "wb") as fp:
|
|
|
+ image.save(fp, format="JPEG", quality=85) # Adjust quality as needed
|
|
|
+ print(f"snap: {jpeg_filename}")
|
|
|
+
|
|
|
+
|
|
|
+def save_screenshot2(path="test"):
|
|
|
+ proc = subprocess.Popen(
|
|
|
+ "adb exec-out screencap -p", shell=True, stdout=subprocess.PIPE
|
|
|
+ )
|
|
|
+ image_bytes = proc.stdout.read()
|
|
|
+ image = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR)
|
|
|
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
+
|
|
|
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
+ jpeg_filename = f"{path}/{timestamp}.jpg"
|
|
|
+
|
|
|
+ cv2.imwrite(
|
|
|
+ jpeg_filename,
|
|
|
+ cv2.cvtColor(image, cv2.COLOR_RGB2BGR),
|
|
|
+ [int(cv2.IMWRITE_JPEG_QUALITY), 85],
|
|
|
+ )
|
|
|
+ print(f"snap: {jpeg_filename}")
|
|
|
+
|
|
|
+
|
|
|
client = AdbClient(host="127.0.0.1", port=5037)
|
|
|
device = client.device(android_address)
|
|
|
|