<commit_before>
import io
import os
import time
from threading import Thread
from itertools import cycle

import numpy as np

from PIL import Image

from donkey import utils

 
class BaseCamera:

    def __init__(self, resolution=(160, 120)):
        self.resolution = resolution
        self.frame = np.zeros(shape=(self.resolution[1], self.resolution[0], 3))
        
    def start(self):
        # start the thread to read frames from the video stream
        t = Thread(target=self.update, args=())
        t.daemon = True
        t.start()
        time.sleep(1)
        return self

    def update(self):
        while True:
            pass

    def read(self):
        return self.frame

    def capture_arr(self):
        return self.read()

    def capture_img(self):
        arr = self.capture_arr()
        print(type(arr))
        img = Image.fromarray(arr, 'RGB')
        return img
        
    def capture_binary(self):
        img = self.capture_img()
        return utils.img_to_binary(img)

class WebcamVideoStream(BaseCamera):
    def __init__(self, resolution=(160, 120), framerate=20):
        import pygame
        import pygame.camera

        pygame.init()
        pygame.camera.init()
        l = pygame.camera.list_cameras()
        self.cam = pygame.camera.Camera(l[0], resolution, "RGB")
        self.cam.start()
        self.framerate = framerate

        # initialize the frame and the variable used to indicate
        # if the thread should be stopped
        self.frame = None
        self.stopped = False

        print('WebcamVideoStream loaded.. .warming camera')

        time.sleep(2)
        self.start()

    def update(self):
        from datetime import datetime, timedelta
        import pygame.image
        while not self.stopped:
            start = datetime.now()

            if self.cam.query_image():
                # snapshot = self.cam.get_image()
                # self.frame = list(pygame.image.tostring(snapshot, "RGB", False))
                snapshot = self.cam.get_image()
                self.frame = pygame.surfarray.pixels3d(pygame.transform.rotate(pygame.transform.flip(snapshot, True, False), 90))

            stop = datetime.now()
            s = 1 / self.framerate - (stop - start).total_seconds()
            if s > 0:
                time.sleep(s)

        self.cam.stop()

class PiVideoStream(BaseCamera):
    def __init__(self, resolution=(160, 120), framerate=20):
        from picamera.array import PiRGBArray
        from picamera import PiCamera

        # initialize the camera and stream
        self.camera = PiCamera()
        self.camera.resolution = resolution
        self.camera.framerate = framerate<commit_msg>Apparently my webcam can not do the small resolution, do it explicitly<commit_after>
import io
import os
import time
from threading import Thread
from itertools import cycle

import numpy as np

from PIL import Image

from donkey import utils

 
class BaseCamera:

    def __init__(self, resolution=(160, 120)):
        self.resolution = resolution
        self.frame = np.zeros(shape=(self.resolution[1], self.resolution[0], 3))
        
    def start(self):
        # start the thread to read frames from the video stream
        t = Thread(target=self.update, args=())
        t.daemon = True
        t.start()
        time.sleep(1)
        return self

    def update(self):
        while True:
            pass

    def read(self):
        return self.frame

    def capture_arr(self):
        return self.read()

    def capture_img(self):
        arr = self.capture_arr()
        print(type(arr))
        img = Image.fromarray(arr, 'RGB')
        return img
        
    def capture_binary(self):
        img = self.capture_img()
        return utils.img_to_binary(img)

class WebcamVideoStream(BaseCamera):
    def __init__(self, resolution = (160, 120), framerate = 20):
        import pygame
        import pygame.camera

        super().__init__(resolution = resolution)

        pygame.init()
        pygame.camera.init()
        l = pygame.camera.list_cameras()
        self.cam = pygame.camera.Camera(l[0], resolution, "RGB")
        self.cam.start()
        self.framerate = framerate

        # initialize variable used to indicate
        # if the thread should be stopped
        self.stopped = False

        print('WebcamVideoStream loaded.. .warming camera')

        time.sleep(2)
        self.start()

    def update(self):
        from datetime import datetime, timedelta
        import pygame.image
        while not self.stopped:
            start = datetime.now()

            if self.cam.query_image():
                # snapshot = self.cam.get_image()
                # self.frame = list(pygame.image.tostring(snapshot, "RGB", False))
                snapshot = self.cam.get_image()
                snapshot1 = pygame.transform.scale(snapshot, self.resolution)
                self.frame = pygame.surfarray.pixels3d(pygame.transform.rotate(pygame.transform.flip(snapshot1, True, False), 90))

            stop = datetime.now()
            s = 1 / self.framerate - (stop - start).total_seconds()
            if s > 0:
                time.sleep(s)

        self.cam.stop()

    def stop(self):
        # indicate that the thread should be stopped
        self.stopped = True

class PiVideoStream(BaseCamera):
    def __init__(self, resolution=(160, 120), framerate=20):
        from picamera.array import PiRGBArray
        from picamera import PiCamera

        # initialize the camera and stream
        self.camera = PiCamera()
        self.camera.resolution = resolution
        self.camera.framerate = framerate
{'fraction_non_alphanumeric': 0.09576165986877105, 'fraction_numerical': 0.012058875687178578, 'mean_word_length': 2.1143014908890114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '869671', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1564, 'n_words': 428}