def init_cams(self, which_cam_idx):
# gets a list of available cameras.
self.clist = pygame.camera.list_cameras()
print (self.clist)
if not self.clist:
raise ValueError("Sorry, no cameras detected.")
try:
cam_id = self.clist[which_cam_idx]
except IndexError:
cam_id = self.clist[0]
# creates the camera of the specified size and in RGB colorspace
self.camera = pygame.camera.Camera(cam_id, self.size, "RGB")
# starts the camera
self.camera.start()
self.clock = pygame.time.Clock()
# create a surface to capture to. for performance purposes, you want the
# bit depth to be the same as that of the display surface.
self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
python类camera()的实例源码
def get_and_flip(self):
# if you don't want to tie the framerate to the camera, you can check and
# see if the camera has an image ready. note that while this works
# on most cameras, some will never return true.
if 0 and self.camera.query_image():
# capture an image
self.snapshot = self.camera.get_image(self.snapshot)
if 0:
self.snapshot = self.camera.get_image(self.snapshot)
#self.snapshot = self.camera.get_image()
# blit it to the display surface. simple!
self.display.blit(self.snapshot, (0,0))
else:
self.snapshot = self.camera.get_image(self.display)
#self.display.blit(self.snapshot, (0,0))
pygame.display.flip()
def main():
while True:
try:
print ('Saving image from camera...')
start_time_camera = time.time()
img = cam.get_image()
pygame.image.save(img, image_path)
end_time_camera =time.time()
time_dif_camera = end_time_camera - start_time_camera
# Print the time-usage.
os.system('cls' if os.name == 'nt' else 'clear')
print ('###### time usage camera ######')
print(str(timedelta(seconds=int(round(time_dif_camera)))))
except (KeyboardInterrupt, SystemExit, RuntimeError, SystemError):
cam.stop()
def __init__(self, resolution=(120, 160), framerate=20):
from picamera.array import PiRGBArray
from picamera import PiCamera
resolution = (resolution[1], resolution[0])
# initialize the camera and stream
self.camera = PiCamera() #PiCamera gets resolution (height, width)
self.camera.resolution = resolution
self.camera.framerate = framerate
self.rawCapture = PiRGBArray(self.camera, size=resolution)
self.stream = self.camera.capture_continuous(self.rawCapture,
format="rgb", use_video_port=True)
# initialize the frame and the variable used to indicate
# if the thread should be stopped
self.frame = None
self.on = True
print('PiCamera loaded.. .warming camera')
time.sleep(2)
def __init__(self, resolution = (160, 120), framerate = 20):
import pygame
import pygame.camera
super().__init__()
pygame.init()
pygame.camera.init()
l = pygame.camera.list_cameras()
self.cam = pygame.camera.Camera(l[0], resolution, "RGB")
self.resolution = resolution
self.cam.start()
self.framerate = framerate
# initialize variable used to indicate
# if the thread should be stopped
self.frame = None
self.on = True
print('WebcamVideoStream loaded.. .warming camera')
time.sleep(2)
def __init__(self, **argd):
self.__dict__.update(**argd)
super(VideoCapturePlayer, self).__init__(**argd)
# create a display surface. standard pygame stuff
self.display = pygame.display.set_mode( self.size, 0 )
# gets a list of available cameras.
self.clist = pygame.camera.list_cameras()
if not self.clist:
raise ValueError("Sorry, no cameras detected.")
# creates the camera of the specified size and in RGB colorspace
self.camera = pygame.camera.Camera(self.clist[0], self.size, "RGB")
# starts the camera
self.camera.start()
self.clock = pygame.time.Clock()
# create a surface to capture to. for performance purposes, you want the
# bit depth to be the same as that of the display surface.
self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
def oneSave(angleStep): #=angle
global sMat, bb, fp
rr=0
#x=startx #camera picture X
y=sTop #camera picture Y
#---export xyz--- to filename.xyz
y=sTop+1
while y<height-sBott:
xx = sMat[y-sTop][angleStep]
if xx!=0 and xx>-200:
angle=float(2*pi/(loop-1)*angleStep)
rx=float(math.sin(angle)*xx*nasDef)
ry=float(math.cos(angle)*xx*nasDef)
rz = y
co = str(rx)+" "+str(ry)+" "+str(rz)
#cop = str(angle)+" > "+str(xx)+" > "+co
#print cop
fp.write(co+"\n")
y=y+kroky
#time.sleep(0.2)
def main():
pygame.init()
pygame.camera.init()
VideoCapturePlayer().main()
pygame.quit()
def shutdown(self):
# indicate that the thread should be stopped
self.on = False
print('stoping PiCamera')
time.sleep(.5)
self.stream.close()
self.rawCapture.close()
self.camera.close()
def get_and_flip(self):
# if you don't want to tie the framerate to the camera, you can check and
# see if the camera has an image ready. note that while this works
# on most cameras, some will never return true.
if 0 and self.camera.query_image():
# capture an image
self.snapshot = self.camera.get_image(self.snapshot)
self.snapshot = self.camera.get_image(self.snapshot)
#self.snapshot = self.camera.get_image()
# blit it to the display surface. simple!
self.display.blit(self.snapshot, (0,0))
pygame.display.flip()
def main():
pygame.init()
pygame.camera.init()
VideoCapturePlayer().main()
pygame.quit()
def info(self):
return [self.NAME,
"Collecting data from camera"]
def __init__(self, chunk_size=239):
pygame.camera.init()
available_cameras = pygame.camera.list_cameras()
if not available_cameras:
raise Exception("No camera available")
self.cam = pygame.camera.Camera(available_cameras[0])
self.last_raw = None
self.last_time = None
self.chunk_size = chunk_size
super().__init__(None)
def main():
# Load the Inception model so it is ready for classifying images.
try:
model = inception.Inception()
except FileNotFoundError:
print ('###### warning ######')
print ('this script requires inception.maybe_download() executed at least once, running it now')
inception.maybe_download()
model = inception.Inception()
while True:
try:
start_time_camera = time.time()
cam.start()
# start and stop prevent the buffer from filling up with more useless frames
img = cam.get_image()
cam.stop()
image = pygame.surfarray.array3d(img)
image = np.rot90(image, 3)
end_time_camera = time.time()
start_time = time.time()
print ("Classifying image from camera...")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
classify(model=model, image=image)
end_time = time.time()
time_dif_camera = end_time_camera - start_time_camera
time_dif = end_time - start_time
# Print the time-usage.
print ('###### time usage camera ######')
print(str(timedelta(seconds=int(round(time_dif_camera)))))
print ('###### time usage NN ######')
print(str(timedelta(seconds=int(round(time_dif)))))
# Save the image that was just classified (for debug)
im = Image.fromarray(image)
im.save(image_path)
except (KeyboardInterrupt, SystemExit, RuntimeError, SystemError):
cam.stop()
model.close()
def main():
# Load the Inception model so it is ready for classifying images.
try:
model = inception.Inception()
except FileNotFoundError:
print ('###### warning ######')
print ('this script requires inception.maybe_download() executed at least once, running it now')
inception.maybe_download()
model = inception.Inception()
while True:
try:
start_time_camera = time.time()
# multiple times to empty the buffer
img = cam.get_image()
img = cam.get_image()
img = cam.get_image()
image = pygame.surfarray.array3d(img)
image = np.fliplr(np.rot90(image, 3))
if debug:
# Save the image that was just classified (for debug)
im = Image.fromarray(image)
im.save(image_path)
end_time_camera = time.time()
start_time = time.time()
#print ("Classifying image from camera...")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
classify(model=model, image=image)
end_time = time.time()
time_dif_camera = end_time_camera - start_time_camera
time_dif = end_time - start_time
# Print the time-usage.
out_file.write('###### time usage camera ######\n')
out_file.write(str(timedelta(seconds=int(round(time_dif_camera))))+"\n")
out_file.write('###### time usage NN ######\n')
out_file.write(str(timedelta(seconds=int(round(time_dif))))+"\n")
out_file.flush()
os.fsync(out_file)
print('###### time usage camera ######')
print(str(timedelta(seconds=int(round(time_dif_camera)))))
print('###### time usage NN ######')
print(str(timedelta(seconds=int(round(time_dif)))))
except (KeyboardInterrupt, SystemExit, RuntimeError, SystemError):
cam.stop()
model.close()
out_file.close()