I am using pygame to create my own GUI and which shows the camera preview on LCD. Meantime I want to save the camera recording in the file.
For this reason I capture the stream in JPEG format, than use numpy read into buffer in JPEG format and than using cv2.decode decode it to image. After that I am able to write it as a video.
But pygame module cannot accept numpy object, it needs surface element. And I don’t know how to convert the imageData to surface object, for pygame.image.fromstring can use it.
Please help to provide JPEG image to pygame.image.fromstring object.
pygame.init()
screen = pygame.display.set_mode((LCD_W,LCD_H))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output2.avi',fourcc, 30.0, (640,480))
while (True):
camera.capture(stream, use_video_port=True, format='jpeg')
stream.seek(0)
imageData = np.fromstring(stream.getvalue(), dtype=np.uint8)
npImage = cv2.imdecode(imageData, 1)
out.write(npImage)
screen.blit(npImage, # gives error on npImage type
((LCD_W - img.get_width() ) / 2,
(LCD_H - img.get_height()) / 2))
img = pygame.image.fromstring( # gives ValueError saying cannot read
stream.getvalue(),
sizeData[sizeMode][1], 'RGB')
stream.close()