-
Notifications
You must be signed in to change notification settings - Fork 37
/
photomod.py
executable file
·483 lines (348 loc) · 12.5 KB
/
photomod.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
from __future__ import print_function
import time
from time import sleep
import datetime
import os
import sys
import subprocess
from PIL import Image # to make thumbnail
from subprocess import call
from shutil import copyfile
def videodevlist():
folderpath="/dev"
videolist=[]
filelist=os.listdir(folderpath)
for filename in filelist:
if "video" in filename:
# following code was necessary starting from raspbian buster, by default there are video10,video11,video12 devices created
#get the video number
videonumberstr=get_digits(filename)
try:
videonumber=int(videonumberstr)
except:
videonumber=-1
print("not able to convert the video number")
if videonumber>-1:
if (videonumber<10):
print("check video " , filename)
# following code was necessary starting from raspbian buster, linux kernel v4l2 was updated, now one single webcam can show more than one videoXX dev
if checkvideoformatexist(videonumberstr):
print("OK video " , filename)
videolist.append(filename)
return videolist # item1 (path) item2 (name) item3 (datetime)
def get_digits(x):
return ''.join(ele for ele in x if ele.isdigit())
def checkvideoformatexist(videonumberstr):
# v4l2-ctl -d /dev/video0 -D
DeviceType="Video Capture"
formats=['YU12','YUYV','RGB3','JPEG','H264','MJPG','YVYU','VYUY','UYVY','NV12','BGR3','YV12','NV21','BGR4']
# v4l2-ctl --list-formats -d 1
cmd = ['v4l2-ctl', '--list-formats' , '-d', videonumberstr]
try:
scanoutput = subprocess.check_output(cmd).decode('utf-8')
except:
print("error to execute the command" , cmd)
logger.error("error to execute the command %s",cmd)
return False
if not DeviceType in scanoutput:
print("not a video capture device =" , videonumberstr)
return False
# check if one of the format is inside the output string
for formatitem in formats:
if formatitem in scanoutput:
print("At least a format = ", formatitem ," for video capture device =" , videonumberstr)
return True
return False
def checkPIcam(device):
cmd = ['v4l2-ctl', '-d', '/dev/'+device, '-D']
wordtofind="bm2835"
isfound=executeandsearch(cmd,wordtofind)
return isfound
def findPIcam():
devicelist=videodevlist()
# v4l2-ctl -d /dev/video0 -D
for device in devicelist:
cmd = ['v4l2-ctl', '-d', '/dev/'+device, '-D']
wordtofind="bm2835"
isfound=executeandsearch(cmd,wordtofind)
if isfound:
return device
return ""
def executeandsearch(cmd,wordtofind):
try:
scanoutput = subprocess.check_output(cmd).decode('utf-8')
except:
print("error to execute the command" , cmd)
logger.error("error to execute the command %s",cmd)
return False
for line in scanoutput.split('\n'):
#print " line ",line
strstart=line.find(wordtofind)
if strstart>-1:
#found
return True
return False
def saveshot(filepath, video, realshot, resolution, positionvalue, vdirection):
shottaken=False
print("take photo")
if vdirection=="neg":
rotdeg="180"
else:
rotdeg="0"
currentdate=datetime.datetime.now().strftime("%y-%m-%d,%H:%M")
print("Current date and time: " , currentdate)
if realshot:
filenamenopath=currentdate+"@"+video+"@"+positionvalue+".jpg"
filenamenopath2=currentdate+"@"+video+"@"+positionvalue+"F.jpg"
filenamenopath3=currentdate+"@"+video+"@"+positionvalue+"R.jpg"
else:
filenamenopath="testimage.jpg"
filenamenopath2=filenamenopath
filenamenopath3=filenamenopath
filename=os.path.join(filepath, filenamenopath)
print("Start Photo procedure: ", video , " ************************************************")
print("FILE : ", filename)
cam_list = "/dev/" + video
if not (video==""):
filexist=os.path.isfile(filename)
print("file already exist = ", filexist)
if (filexist)and(not realshot):
os.rename(filename, filename + ".bak")
shottaken=False
w=resolution.split("x")[0]
h=resolution.split("x")[1]
filenamebase=filenamenopath.split(".")[0]
extension=filename.split(".")[1]
# capture image using V4l2
# http://www.geeetech.com/wiki/index.php/Raspberry_Pi_Camera_Module
# v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
# v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
# https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=62364&start=450
# check the v4l2 setting: v4l2-ctl -d /dev/video0 --list-ctrls
# setting
# v4l2-ctl --set-ctrl=gain=00
# sudo v4l2-ctl -d /dev/video0 --set-ctrl=auto_exposure=1
# (auto exposure=0 ->auto; exposure=1 ->manual, each camera has its own name of the parameter, auto_exposure, exposure_auto)
# v4l2-ctl --set-ctrl=exposure_absolute=10
# raspistill provides way better photo than the fswebcam when using the raspbery camera
# raspberry camera is on video0 only
# there is no reliable way to detect the raspicam, then just try to get a picture first with raspistill
if checkPIcam(video):
print("The video device should be PI camera")
shottaken=takeshotandsave_raspistill(filepath,filenamenopath3, video, resolution,rotdeg)
if not shottaken: # gives it a second chance :)
shottaken=takeshotandsave_fswebcam(filepath,filenamenopath2, video, resolution,rotdeg)
else:
print(" The video device should be USB camera")
shottaken=takeshotandsave_fswebcam(filepath,filenamenopath2, video, resolution,rotdeg)
#shottaken=takeshotandsave_mjpg_streamer(filepath,filenamenopath, video, resolution)
if (not shottaken)and(not realshot):
if filexist:
os.rename(filename + ".bak", filename)
print("Picture acknowledge return = " ,shottaken)
else:
print("camera not connected")
return shottaken
def takeshotandsave_raspistill(filepath,filenamenopath, video, resolution, rotdeg):
shottaken=False
if rotdeg=="180":
vflip="-vf -hf"
else:
vflip=""
print("flip ", vflip)
cam_list = "/dev/" + video
i=0
while (not shottaken)and(i<3):
i=i+1
filename=os.path.join(filepath, filenamenopath)
print("FILE : ", filename)
shottaken=False
w=resolution.split("x")[0]
h=resolution.split("x")[1]
print("try raspistill")
filenamebase=filenamenopath.split(".")[0]
extension=filename.split(".")[1]
# create the picture files
isok=False
try:
myproc = subprocess.check_output("raspistill "+vflip+" -w "+w+" -h "+h+" -q 95 -a 12 -a \"%Y-%m-%d %X (UTC)\" -o " + filename, shell=True, stderr=subprocess.STDOUT)
isok=True
except:
print("problem to execute command")
newfilexist=os.path.isfile(filename)
print("file was created = ", newfilexist)
if (isok)and(newfilexist):
print("raspistill got picture")
shottaken=True
# make thumbnail
ExistandThumb(filepath,filenamenopath,shottaken)
else:
print("raspistill not able to get picture")
shottaken=False
print("RASPISTILL Picture take = " ,shottaken, " Attempt ", i)
return shottaken
def takeshotandsave_fswebcam(filepath,filenamenopath, video, resolution, rotdeg):
shottaken=False
if not (video==""):
cam_list = "/dev/" + video
i=0
while (not shottaken)and(i<3):
i=i+1
filename=os.path.join(filepath, filenamenopath)
print("FILE : ", filename)
shottaken=False
w=resolution.split("x")[0]
h=resolution.split("x")[1]
print("try fswebcam")
filenamebase=filenamenopath.split(".")[0]
extension=filename.split(".")[1]
#fswebcam option
if i==1:
S="15"
else:
S="5"
# create the picture files
#fswebcam option
isok=False
try:
myproc = subprocess.check_output("fswebcam -q -d "+ cam_list +" -r "+resolution+" -S "+S+" --rotate "+rotdeg+" -s brightness=50% -s Contrast=50% --jpeg 95 " + filename, shell=True, stderr=subprocess.STDOUT)
isok=True
except:
print("problem to execute command")
# -R use read() method -- NOT WORKING ---
# -D delay before taking frames
# -S skip the first frames
# -q quiet output mode
# -d device
# -r resoltion
# -F takes frames
print("output from subprocess: " , myproc)
newfilexist=os.path.isfile(filename)
print("file was created = ", newfilexist)
if (isok)and(newfilexist):
print("fswebcam got picture")
shottaken=True
# make thumbnail
ExistandThumb(filepath,filenamenopath,shottaken)
else:
print("fswebcam not able to get picture")
shottaken=False
print("FSWEBCAM Picture take = " ,shottaken, " Attempt ", i)
else:
print("camera not connected")
return shottaken
def takeshotandsave_mjpg_streamer(filepath,filenamenopath, video, resolution , rotdeg):
shottaken=False
filename=os.path.join(filepath, filenamenopath)
print("FILE : ", filename)
if not (video==""):
cam_list = "/dev/" + video
shottaken=False
w=resolution.split("x")[0]
h=resolution.split("x")[1]
print("try mjpg_streamer")
filenamebase=filenamenopath.split(".")[0]
extension=filename.split(".")[1]
pathmjpg=os.path.join(filepath,"mjpg")
if not os.path.exists(pathmjpg):
# fi folder do not exist, create it
os.makedirs(pathmjpg)
else:
#remove all files in folder
for the_file in os.listdir(pathmjpg):
file_path = os.path.join(pathmjpg, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
# create the picture files
fps="20"
if (video=="video0")and(int(w)>1024):
print("mjpg_streamer using the raspicam")
stream="mjpg_streamer -i '/usr/local/lib/mjpg-streamer/input_raspicam.so -d /dev/"+video+" -x "+w+" -y "+h+" -fps "+fps+" -rot "+rotdeg+"' -o '/usr/local/lib/mjpg-streamer/output_file.so -f "+pathmjpg+" -d 100' &"
else:
stream="mjpg_streamer -i '/usr/local/lib/mjpg-streamer/input_uvc.so -d /dev/"+video+" -r "+w+"x"+h+" -f "+fps+" -rot "+rotdeg+"' -o '/usr/local/lib/mjpg-streamer/output_file.so -f "+pathmjpg+" -d 100' &"
call ([stream], shell=True)
time.sleep(2)
call (["sudo pkill mjpg_streamer"], shell=True)
# take last saved file in the folder
folderpath=pathmjpg
filenamelist=[]
sortedlist=sorted([f for f in os.listdir(folderpath) if os.path.isfile(os.path.join(folderpath, f))])
sortedlist.reverse()
lastfile=""
for files in sortedlist:
if (files.endswith(".jpg") or files.endswith(".png")):
lastfile=files
break
#copy lastfile to filepath
if not (lastfile==""):
shottaken=True
#copy file to the right folder and right name
src=os.path.join(pathmjpg, lastfile)
dst=filename
copyfile(src, dst)
# make thumbnail
ExistandThumb(filepath,filenamenopath,shottaken)
else:
print("mjpg_streame not able to get picture")
shottaken=False
print("MJPG_STREAMER Picture take = " ,shottaken)
else:
print("camera not connected")
return shottaken
def ExistandThumb(filepath,filenamenopath,shottaken):
filename=os.path.join(filepath, filenamenopath)
newfilexist=os.path.isfile(filename)
# make thumbnail
if (shottaken and newfilexist):
paththumb=os.path.join(filepath,"thumb")
if not os.path.exists(paththumb):
os.makedirs(paththumb)
try:
image = Image.open(filename)
image.thumbnail((300, 300))
thumbname=os.path.join(paththumb,filenamenopath)
image.save(thumbname)
except:
print("not able to make thumbnail")
return newfilexist
def thumbconsistency(apprunningpath):
# check if there is a thumbnail without corresponding image
filepath=os.path.join(apprunningpath, "static")
filepath=os.path.join(filepath, "hydropicture")
# control if the folder hydropicture exist otherwise create it
if not os.path.exists(filepath):
os.makedirs(filepath)
print("Hydropicture folder has been created")
paththumb=os.path.join(filepath,"thumb")
if not os.path.exists(paththumb):
os.makedirs(paththumb)
print("Hydropicture thumbnail folder has been created")
filenamelist=os.listdir(filepath)
thumbnamelist=os.listdir(paththumb)
for thumbnail in thumbnamelist:
if thumbnail not in filenamelist:
print("thumbnail has no corresponding image, delete")
os.remove(os.path.join(paththumb, thumbnail))
# create thumbnail in case picture has no coresponding thumbnail
for fileimage in filenamelist:
if os.path.isfile(os.path.join(filepath, fileimage)):
if fileimage not in thumbnamelist:
print("image has no corresponding thumbnail, create")
#create thumbnail
try:
image = Image.open(os.path.join(filepath,fileimage))
image.thumbnail((300, 300))
thumbname=os.path.join(paththumb,os.path.basename(fileimage))
image.save(thumbname)
except:
not "able to make thumbnail"
return True
if __name__ == '__main__':
"""
prova funzioni di camera
"""
print("PI cam device :" , findPIcam())
#saveshot()