90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
#!/usr/bin/python
|
|
"""
|
|
#
|
|
# DISPENSION CONFIDENTIAL
|
|
#
|
|
# [2020] - [2021] Dispension Industries Limited.
|
|
# Portions Copyright © 2014-2020 Atlantean Technical Solutions Limited
|
|
# with full rights granted to Dispension & Successors.
|
|
#
|
|
# All Rights Reserved.
|
|
#
|
|
# NOTICE: All information contained herein is, and remains
|
|
# the property of Dispension Industries Limited.
|
|
# The intellectual and technical concepts contained
|
|
# herein are proprietary to Dispension Industries Limited
|
|
# and its suppliers and may be covered by U.S. and Foreign Patents,
|
|
# patents in process, and are protected by trade secret or copyright law.
|
|
# Dissemination of this information or reproduction of this material
|
|
# is strictly forbidden unless prior written permission is obtained
|
|
# from Dispension Industries Limited.
|
|
#
|
|
|
|
Read a Pi camera
|
|
Changed to a py3 Class with better error reporting and UKDI compliance - Carl Goodwin Jan 2021.
|
|
sudo apt install python3-opencv
|
|
sudo pip3 install pyusb
|
|
"""
|
|
import sys
|
|
import numpy as np
|
|
import cv2
|
|
import base64
|
|
|
|
|
|
class Camera(object):
|
|
|
|
# get this from lsusb -vd VEND:PROD
|
|
CAM = None
|
|
frame = None
|
|
|
|
def init(self):
|
|
print("NOOP")
|
|
|
|
|
|
def acquire(self, strcam, filename, test=False):
|
|
MAX = 20
|
|
cam = int(strcam)
|
|
cap = cv2.VideoCapture(cam)
|
|
if not cap.isOpened():
|
|
return '{ "status":4772, "remark":"Could not open camera %d" }' % (cam)
|
|
for i in range(MAX):
|
|
ret, self.frame = cap.read()
|
|
if ret!=True:
|
|
return '{ "status":4773, "remark":"Could not grab frame " }'
|
|
pixel_avg = cv2.cvAvg(self.frame)
|
|
if pixel_avg > 100:
|
|
break
|
|
cap.release()
|
|
ret, buffer = cv2.imencode('.png', self.frame)
|
|
if filename is not None:
|
|
cv2.imwrite(filename)
|
|
cv2.waitKey(0)
|
|
cvs.destroyAllWindows()
|
|
if test:
|
|
cv2.imshow("test_window",self.frame)
|
|
data64 = base64.b64encode(buffer)
|
|
return(str(data64,'utf-8'))
|
|
|
|
|
|
# return a PNG version of the pic in memory
|
|
def png(self):
|
|
_ , im_arr = cv2.imencode('.png', self.frame)
|
|
img_as_txt = base64.b64encode(im_arr)
|
|
return b'data:image/png;base64, '+img_as_txt
|
|
|
|
|
|
# return a JPG version of the pic in memory
|
|
def jpg(self):
|
|
_ , im_arr = cv2.imencode('.jpg', self.frame)
|
|
img_as_txt = base64.b64encode(im_arr)
|
|
return b'data:image/jpeg;base64,'+img_as_txt
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
m = Camera()
|
|
m.acquire(0,"/tmp/newpic.jpg",True)
|
|
|