<commit_before>import time
import io
import threading
import picamera
import datetime


class Camera(object):
	streamThread = None  # background thread that reads frames from camera
	frame = None  # current frame is stored here by background thread
	last_access = 0  # time of last client access to the camera

	def get_frame(self):
		Camera.last_access = time.time()
		if Camera.streamThread is None:
			# start background frame thread
			Camera.streamThread = threading.Thread(target=self._thread)
			Camera.streamThread.start()

			# wait until frames start to be available
			while Camera.frame is None:
				time.sleep(0)
		return Camera.frame

	@classmethod
	def _thread(cls):
		with picamera.PiCamera() as camera:
			# camera setup
			camera.resolution = (832, 624)
			camera.rotation = 270
			camera.hflip = True
			camera.vflip = True
			camera.annotate_background = picamera.Color('black')
			
			# let camera warm up
			camera.start_preview()
			time.sleep(2)
			
			fileStream = picamera.PiCameraCircularIO(camera,seconds = 20)

			networkStream = io.BytesIO()
			for foo in camera.capture_continuous(networkStream, 'jpeg',
													use_video_port=True):
														 
				# store frame
				networkStream.seek(0)
				camera.annotate_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
				cls.frame = networkStream.read()

				# reset stream for next frame
				networkStream.seek(0)
				networkStream.truncate()

				# if there hasn't been any clients asking for frames in
				# the last 10 seconds stop the thread
				if time.time() - cls.last_access > 10:
					break
			
		cls.streamThread = None<commit_msg>use single instance of PiCamera in Camera class<commit_after>import time
import io
import threading
import picamera
import datetime


class Camera(object):
	streamThread = None  # background thread that reads frames from camera
	frame = None  # current frame is stored here by background thread
	last_access = 0  # time of last client access to the camera
	camera = None
	
	def __init__(self):
		# camera setup
		print '__init__'
		Camera.camera = picamera.PiCamera()
		Camera.camera.resolution = (832, 624)
		Camera.camera.rotation = 270
		Camera.camera.hflip = True
		Camera.camera.vflip = True
		Camera.camera.annotate_background = picamera.Color('black')
		time.sleep(2)
		Camera.camera.start_preview()
		
	def get_frame(self):
		Camera.last_access = time.time()
		if Camera.streamThread is None:
			# start background frame thread
			Camera.streamThread = threading.Thread(target=self._streamThread)
			Camera.streamThread.start()

			# wait until frames start to be available
			while Camera.frame is None:
				time.sleep(0)
		return Camera.frame

	@classmethod
	def _streamThread(cls):
		# let camera warm up
		cls.camera.start_preview()
		time.sleep(2)
		networkStream = io.BytesIO()
		for foo in cls.camera.capture_continuous(networkStream, 'jpeg',
												use_video_port=True):
													 
			# store frame
			networkStream.seek(0)
			cls.camera.annotate_text = datetime.datetime.now().strftime('%Y-%m-%d %I:%M:%S %p')
			cls.frame = networkStream.read()

			# reset stream for next frame
			networkStream.seek(0)
			networkStream.truncate()

			# if there hasn't been any clients asking for frames in
			# the last 10 seconds stop the thread
			if time.time() - cls.last_access > 10:
				break
			
		cls.streamThread = None
{'fraction_non_alphanumeric': 0.08972460763991709, 'fraction_numerical': 0.011548711874444773, 'mean_word_length': 3.450592885375494, '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': '2165694', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1006, 'n_words': 302}