-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for Video Games as a Medium of Control/Recording #1193
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import logging | ||
import os | ||
import time | ||
import threading | ||
import numpy as np | ||
from PIL import Image | ||
import mss | ||
import glob | ||
from donkeycar.utils import rgb2gray | ||
|
||
|
@@ -329,6 +331,61 @@ def shutdown(self): | |
self.running = False | ||
time.sleep(0.5) | ||
|
||
class ScreenCamera(BaseCamera): | ||
''' | ||
Camera that uses mss to capture the screen | ||
For capturing video games | ||
''' | ||
|
||
def __init__(self, image_w=160, image_h=120, image_d=3, | ||
vflip=False, hflip=False): | ||
self.image_w = image_w | ||
self.image_h = image_h | ||
self.image_d = image_d | ||
self.vflip = vflip | ||
self.hflip = hflip | ||
self.sct = mss.mss() | ||
self.running = True | ||
|
||
self._monitor_thread = threading.Thread(target=self.take_screenshot, args=()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please have a look at other threaded parts. Threads are not started in the constructor but centrally in the car application, when adding the part as a threaded part - which is what you want to do for your part. |
||
self._monitor_thread.daemon = True | ||
self._monitor_thread.start() | ||
|
||
def take_screenshot(self): | ||
# Capture the screen | ||
monitor = {"top": 0, | ||
"left": 320, | ||
"width": 1920, | ||
"height": 1080 | ||
} | ||
sct_img = self.sct.grab(monitor) | ||
img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX') | ||
img = img.resize((self.image_w, self.image_h)) | ||
img_arr = np.asarray(img) | ||
if self.vflip: | ||
img_arr = np.flipud(img_arr) | ||
if self.hflip: | ||
img_arr = np.fliplr(img_arr) | ||
self.frame = img_arr | ||
return img | ||
|
||
def update(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method should not return but refresh the class member |
||
if self.running: | ||
img = self.take_screenshot() | ||
return img | ||
|
||
def run(self): | ||
self.update() | ||
assert self.frame is not None | ||
return self.frame | ||
|
||
def run_threaded(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to overwrite. |
||
return self.frame | ||
|
||
def shutdown(self): | ||
self.running = False | ||
self.sct.close() | ||
|
||
|
||
class MockCamera(BaseCamera): | ||
''' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have a look at the failed tests, you need to add the python packages into setup.cfg in the right place. Likely we only want to support that in the PC installation but not on the robot.