Skip to content

Instantly share code, notes, and snippets.

@zrt
Created January 2, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrt/f19c80f86b24a25655234850d6eaca05 to your computer and use it in GitHub Desktop.
Save zrt/f19c80f86b24a25655234850d6eaca05 to your computer and use it in GitHub Desktop.
operating raspi camera
#! /usr/bin/env python3
import os
import time
PIC = False
CV2 = False
try:
from picamera import PiCamera
PIC = True
except Exception:
pass
if not PIC:
try:
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise(Exception("Can not open camera through cv2"))
CV2 = True
except Exception:
pass
if not PIC and not CV2:
raise(Exception("Can not open camera through cv2"))
from datetime import datetime
base = os.path.dirname(os.path.realpath(__file__))
counter = 0
def cat_cv2(name):
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imwrite(name, frame)
cap.release()
def cat_pic(name):
with PiCamera() as c:
c.start_preview()
time.sleep(2)
c.capture(name)
def cat():
global counter
counter += 1
name = os.path.join(base, "pics", "%s_%s.jpg" % (time.time(), str(datetime.now())))
if CV2:
cat_cv2(name)
elif PIC:
cat_pic(name)
return name
if __name__ == "__main__":
print(cat())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment