-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
take photo,record video,continuous shoot,timelapse photography,panorama
- Loading branch information
dino
committed
Oct 28, 2021
1 parent
81c3cd8
commit 9217d54
Showing
5 changed files
with
568 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from time import sleep,strftime,localtime | ||
from vilib import Vilib | ||
from sunfounder_io import PWM,Servo,I2C | ||
|
||
import sys | ||
import tty | ||
import termios | ||
|
||
# region read keyboard | ||
def readchar(): | ||
fd = sys.stdin.fileno() | ||
old_settings = termios.tcgetattr(fd) | ||
try: | ||
tty.setraw(sys.stdin.fileno()) | ||
ch = sys.stdin.read(1) | ||
finally: | ||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | ||
return ch | ||
|
||
manual = ''' | ||
Press keys on keyboard to record value! | ||
W: up | ||
A: left | ||
S: right | ||
D: down | ||
Q: continuous_shooting | ||
G: Quit | ||
''' | ||
# endregion | ||
|
||
# region init | ||
I2C().reset_mcu() | ||
sleep(0.01) | ||
|
||
pan = Servo(PWM("P1")) | ||
tilt = Servo(PWM("P0")) | ||
panAngle = 0 | ||
tiltAngle = 0 | ||
|
||
pan.angle(0) | ||
tilt.angle(0) | ||
|
||
#endregion init | ||
|
||
# region servo control | ||
def limit(x,min,max): | ||
if x > max: | ||
return max | ||
elif x < min: | ||
return min | ||
else: | ||
return x | ||
|
||
def servo_control(key): | ||
global panAngle,tiltAngle | ||
if key == 'w': | ||
tiltAngle -= 1 | ||
tiltAngle = limit(tiltAngle, -90, 90) | ||
tilt.angle(tiltAngle) | ||
if key == 's': | ||
tiltAngle += 1 | ||
tiltAngle = limit(tiltAngle, -90, 90) | ||
tilt.angle(tiltAngle) | ||
if key == 'a': | ||
panAngle += 1 | ||
panAngle = limit(panAngle, -90, 90) | ||
pan.angle(panAngle) | ||
if key == 'd': | ||
panAngle -= 1 | ||
panAngle = limit(panAngle, -90, 90) | ||
pan.angle(panAngle) | ||
|
||
# endregion | ||
|
||
# continuous shooting | ||
def continuous_shooting(path,interval_ms:int=50,number=10): | ||
|
||
for i in range(number): | ||
Vilib.take_photo(photo_name='%03d'%i,path=path+'/'+strftime("%Y-%m-%d-%H.%M.%S", localtime())) | ||
print("take_photo: %s"%i) | ||
|
||
def main(): | ||
|
||
Vilib.camera_start(inverted_flag=True) | ||
Vilib.display(local=True,web=True) | ||
|
||
path = "/home/pi/picture/continuous_shooting" | ||
|
||
print(manual) | ||
while True: | ||
key = readchar() | ||
# servo control | ||
servo_control(key) | ||
# take photo | ||
if key == 'q': | ||
print("continuous_shooting .. ") | ||
continuous_shooting(path,interval_ms=50,number=10) | ||
print("continuous_shooting done ") | ||
# esc | ||
if key == 'g': | ||
Vilib.camera_close() | ||
break | ||
|
||
sleep(0.1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
from time import perf_counter, sleep,strftime,localtime | ||
from vilib import Vilib | ||
from sunfounder_io import PWM,Servo,I2C | ||
import cv2 | ||
import os | ||
|
||
import sys | ||
import tty | ||
import termios | ||
|
||
# region read keyboard | ||
def readchar(): | ||
fd = sys.stdin.fileno() | ||
old_settings = termios.tcgetattr(fd) | ||
try: | ||
tty.setraw(sys.stdin.fileno()) | ||
ch = sys.stdin.read(1) | ||
finally: | ||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | ||
return ch | ||
|
||
manual = ''' | ||
Press keys on keyboard to record value! | ||
W: up | ||
A: left | ||
S: right | ||
D: down | ||
Q: take photo | ||
G: Quit | ||
''' | ||
# endregion | ||
|
||
# # check dir | ||
def check_dir(dir): | ||
if not os.path.exists(dir): | ||
try: | ||
os.makedirs(dir) | ||
except Exception as e: | ||
print(e) | ||
|
||
I2C().reset_mcu() | ||
sleep(0.01) | ||
|
||
pan = Servo(PWM("P1")) | ||
tilt = Servo(PWM("P0")) | ||
pan.angle(0) | ||
tilt.angle(0) | ||
panAngle = 0 | ||
tiltAngle = 0 | ||
|
||
''' | ||
https://docs.opencv.org/3.4.15/d2/d8d/classcv_1_1Stitcher.html | ||
https://github.com/opencv/opencv/blob/4.2.0/samples/python/stitching.py | ||
https://github.com/opencv/opencv/blob/4.2.0/samples/python/stitching_detailed.py | ||
''' | ||
Status_info = { | ||
0: 'OK', | ||
1: 'ERR_NEED_MORE_IMGS', | ||
2: 'ERR_HOMOGRAPHY_EST_FAIL', | ||
3: 'ERR_CAMERA_PARAMS_ADJUST_FAIL', | ||
} | ||
|
||
def panorama_shooting(path): | ||
global panAngle,tiltAngle | ||
|
||
temp_path = "/home/pi/picture/.temp/panorama" | ||
imgs =[] | ||
|
||
# check path | ||
check_dir(path) | ||
|
||
# stitcher = cv2.createStitcher(cv2.Stitcher_SCANS) | ||
stitcher = cv2.Stitcher_create(cv2.Stitcher_SCANS) | ||
|
||
# tilt.angle(0) | ||
# panAngle = -90 | ||
for a in range(panAngle,-91,-5): | ||
panAngle = a | ||
pan.angle(panAngle) | ||
sleep(0.1) | ||
# take photo | ||
num = 0 | ||
for angle in range(-80,80,20): | ||
for a in range(panAngle,angle,1): | ||
panAngle = a | ||
pan.angle(a) | ||
sleep(0.1) | ||
sleep(0.5) | ||
# sleep(0.5) | ||
print(num,angle) | ||
Vilib.take_photo(photo_name='%s'%num,path=temp_path) | ||
sleep(0.2) | ||
num += 1 | ||
|
||
# stitch image | ||
for index in range(num): | ||
imgs.append(cv2.imread('%s/%s.jpg'%(temp_path,index))) | ||
cv2.imshow('test',imgs[index]) | ||
print('imgs num: %s'%len(imgs)) | ||
|
||
status,pano = stitcher.stitch(imgs) | ||
|
||
# imwrite and imshow | ||
print('status: %s , %s'%(status,Status_info[status])) | ||
if status == 0: | ||
cv2.imwrite('%s/%s.jpg'%(path,strftime("%Y-%m-%d-%H.%M.%S", localtime())),pano) | ||
cv2.imshow('panorama',pano) | ||
|
||
|
||
# clear temp | ||
# os.removedirs(temp_path) | ||
os.system('sudo rm -r %s'%temp_path) | ||
|
||
# main | ||
|
||
def main(): | ||
|
||
Vilib.camera_start(inverted_flag=True) | ||
Vilib.display(local=True,web=False) | ||
|
||
path = "/home/pi/picture/panorama" | ||
|
||
print(manual) | ||
while True: | ||
key = readchar() | ||
# take photo | ||
if key == 'q': | ||
print("panorama shooting ...") | ||
panorama_shooting(path) | ||
|
||
# esc | ||
if key == 'g': | ||
print('Quit') | ||
Vilib.camera_close() | ||
break | ||
|
||
sleep(0.01) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
|
||
from time import sleep,strftime,localtime | ||
from vilib import Vilib | ||
from sunfounder_io import PWM,Servo,I2C | ||
|
||
import sys | ||
import tty | ||
import termios | ||
|
||
# region read keyboard | ||
def readchar(): | ||
fd = sys.stdin.fileno() | ||
old_settings = termios.tcgetattr(fd) | ||
try: | ||
tty.setraw(sys.stdin.fileno()) | ||
ch = sys.stdin.read(1) | ||
finally: | ||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | ||
return ch | ||
|
||
manual = ''' | ||
Press keys on keyboard to record value! | ||
W: up | ||
A: left | ||
S: right | ||
D: down | ||
Q: record/pause/continue | ||
E: stop | ||
G: Quit | ||
''' | ||
# endregion | ||
|
||
# region init | ||
I2C().reset_mcu() | ||
sleep(0.01) | ||
|
||
pan = Servo(PWM("P1")) | ||
tilt = Servo(PWM("P0")) | ||
panAngle = 0 | ||
tiltAngle = 0 | ||
pan.angle(0) | ||
tilt.angle(0) | ||
|
||
Vilib.rec_video_set["path"] = "/home/pi/video/" | ||
vname = strftime("%Y-%m-%d-%H.%M.%S", localtime()) | ||
Vilib.rec_video_set["name"] = vname | ||
|
||
rec_flag = 'stop' # start,pause,stop | ||
# endregion init | ||
|
||
# rec control | ||
def rec_control(key): | ||
global rec_flag | ||
|
||
if key == 'q' and rec_flag == 'stop': | ||
key = None | ||
rec_flag = 'start' | ||
Vilib.rec_video_run() | ||
print('rec start ...') | ||
if key == 'q' and rec_flag == 'start': | ||
key = None | ||
rec_flag = 'pause' | ||
Vilib.rec_video_pause() | ||
print('pause') | ||
if key == 'q' and rec_flag == 'pause': | ||
key = None | ||
rec_flag = 'start' | ||
Vilib.rec_video_start() | ||
print('continue') | ||
|
||
if key == 'e' and rec_flag != 'stop': | ||
Vilib.rec_video_stop() | ||
print('stop') | ||
print("The video saved as %s%s.avi"%(Vilib.rec_video_set["path"],vname)) | ||
|
||
# region servo control | ||
def limit(x,min,max): | ||
if x > max: | ||
return max | ||
elif x < min: | ||
return min | ||
else: | ||
return x | ||
|
||
def servo_control(key): | ||
global panAngle,tiltAngle | ||
if key == 'w': | ||
tiltAngle -= 1 | ||
tiltAngle = limit(tiltAngle, -90, 90) | ||
tilt.angle(tiltAngle) | ||
if key == 's': | ||
tiltAngle += 1 | ||
tiltAngle = limit(tiltAngle, -90, 90) | ||
tilt.angle(tiltAngle) | ||
if key == 'a': | ||
panAngle += 1 | ||
panAngle = limit(panAngle, -90, 90) | ||
pan.angle(panAngle) | ||
if key == 'd': | ||
panAngle -= 1 | ||
panAngle = limit(panAngle, -90, 90) | ||
pan.angle(panAngle) | ||
|
||
# endregion servo control | ||
|
||
|
||
def main(): | ||
|
||
Vilib.camera_start(inverted_flag=True) | ||
Vilib.display(local=True,web=True) | ||
|
||
print(manual) | ||
while True: | ||
key = readchar() | ||
# rec control | ||
rec_control(key) | ||
# servo control | ||
servo_control(key) | ||
# esc | ||
if key == 'g': | ||
Vilib.camera_close() | ||
break | ||
|
||
sleep(0.1) | ||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.