-
Notifications
You must be signed in to change notification settings - Fork 2
/
strmcam.py
142 lines (125 loc) · 5.54 KB
/
strmcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def strmcam():
import sys
import os
import time
import subprocess
import logging
# Setup logging
logging.basicConfig(level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(funcName)-10s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# List of valid camera values in the configcam.py file
CAMLIST = ('usbcam', 'rtspcam', 'pilibcam', 'pilegcam')
# Import Required Variables from configcam.py
try:
from configcam import (CAMERA,
IM_SIZE,
RTSPCAM_SRC,
USBCAM_SRC,
IM_FRAMERATE,
IM_ROTATION,
IM_HFLIP,
IM_VFLIP
)
except Exception as e:
logging.error(e)
sys.exit(1)
# ------------------------------------------------------------------------------
def is_pi_legacy_cam():
'''
Determine if pi camera is configured for Legacy = True or Libcam = False.
'''
logging.info("Check for Legacy Pi Camera Module with command - vcgencmd get_camera")
camResult = subprocess.check_output("vcgencmd get_camera", shell=True)
camResult = camResult.decode("utf-8")
camResult = camResult.replace("\n", "")
params = camResult.split()
if params[0].find("=1") >= 1 and params[1].find("=1") >= 1:
logging.info("Pi Camera Module Found %s", camResult)
return True
else:
logging.error("Problem Finding Pi Legacy Camera %s", camResult)
logging.error('Check Camera Connections and Legacy Pi Cam is Enabled per command sudo raspi=config')
return False
# ------------------------------------------------------------------------------
def create_cam_thread(cam_name):
'''
Create the appropriate video stream thread
bassed on the specified camera name
returns vs video stream and updated camera name with source if applicable
'''
cam_name = cam_name.lower()
if not cam_name in CAMLIST:
logging.error('%s Not a Valid Camera Value', cam_name)
logging.info('Valid Values are %s', ' '.join(CAMLIST))
logging.info('Edit configcam.py CAMERA variable.')
sys.exit(1)
cam_title = None
if cam_name == 'pilibcam':
# check if pi libcam
if not is_pi_legacy_cam():
if not os.path.exists('/usr/bin/libcamera-still'):
logging.error('libcamera not Installed')
logging.info('Edit configcam.py and Change CAMERA variable as Required.')
sys.exit(1)
if not os.path.exists('strmpilibcam.py'):
logging.error("strmpilibcam.py File Not Found.")
sys.exit(1)
try:
from strmpilibcam import CamStream
except ImportError:
logging.error("import Failed. from strmpilibcam import CamStream")
sys.exit(1)
cam_title = cam_name
vs = CamStream(size=IM_SIZE,
vflip=IM_VFLIP,
hflip=IM_HFLIP).start()
else:
logging.error('Looks like Pi Legacy Camera is Enabled')
logging.info('Edit configcam.py and Change CAMERA variable as Required.')
logging.info('or Disable Legacy Pi Camera using command sudo raspi-config')
sys.exit(1)
elif cam_name == 'pilegcam':
# Check if Pi Legacy Camera
if not is_pi_legacy_cam():
sys.exit(1)
# Check if the
if not os.path.exists('strmpilegcam.py'):
logging.error("File Not Found. Could Not Import strmpilegcam.py")
sys.exit(1)
# Now import the Pi Legacy stream library
try:
from strmpilegcam import CamStream
except ImportError:
logging.error("Import Failed. from strmpilegcam import CamStream")
sys.exit(1)
cam_title = cam_name
# Create Legacy Pi Camera Video Stream Thread
vs = CamStream(size=IM_SIZE,
framerate=IM_FRAMERATE,
rotation=IM_ROTATION,
hflip=IM_HFLIP,
vflip=IM_VFLIP).start()
elif cam_name == 'usbcam' or cam_name == 'rtspcam':
if cam_name == 'rtspcam':
cam_src = RTSPCAM_SRC
cam_title = cam_name + ' src= ' + cam_src
elif cam_name == 'usbcam':
cam_src = USBCAM_SRC
cam_title = cam_name + ' src= ' + str(cam_src)
if not os.path.exists('strmusbipcam.py'):
logging.error("File Not Found. Could Not Import strmusbipcam.py")
sys.exit(1)
try:
from strmusbipcam import CamStream
except ImportError:
logging.error("Import Failed. from strmusbipcam import CamStream")
sys.exit(1)
vs = CamStream(src=cam_src, size=IM_SIZE).start()
logging.info("%s Started Video Stream Thread.", cam_title.upper())
return vs
vs = create_cam_thread(CAMERA)
logging.info("Warming Up Camera.")
time.sleep(3) # Allow Camera to warm up
return vs