-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use open camera remote to trigger smartphone camera
- Loading branch information
sitzmann
committed
Feb 1, 2022
1 parent
947d6d3
commit 213b4e4
Showing
6 changed files
with
106 additions
and
3 deletions.
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
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
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,92 @@ | ||
import urllib3 | ||
import socket | ||
import cfg | ||
import cv2 | ||
import time | ||
import re | ||
import cv_utils | ||
import http | ||
from datetime import datetime | ||
from contextlib import contextmanager | ||
import numpy as np | ||
|
||
|
||
def trigger_remote_camera(img_number=0): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | ||
sock.sendto(bytes(f'F', "utf-8"), (cfg.ip, cfg.udp_port)) | ||
time.sleep(cfg.autofocus_time_seconds) # wait for autofocus | ||
sock.sendto(bytes(f'C{img_number}', "utf-8"), (cfg.ip, cfg.udp_port)) | ||
pass | ||
|
||
|
||
def filename_2_number(name): | ||
splitted = name.split('_') | ||
if len(splitted) == 2: | ||
return -1 | ||
else: | ||
num = splitted[-1].split('.')[0] | ||
return int(num) | ||
|
||
|
||
def find_photo(img_number=0): # todo check status? | ||
response = urllib3.PoolManager().request("GET", f'{cfg.ip}:{cfg.http_port}') | ||
if response.status == http.HTTPStatus.OK: | ||
response = str(response.data) | ||
else: | ||
return None | ||
pattern = re.compile(f'>{cfg.img_prefix}{img_number}(?:_\d+)?\.jpg') | ||
matches = pattern.findall(response) | ||
if not matches: | ||
return None | ||
matches = sorted([m[1:] for m in matches], key=filename_2_number) | ||
filename = sorted(matches, key=filename_2_number)[-1] | ||
return filename | ||
|
||
|
||
def download_photo(img_number=0, last_filename=None): | ||
filename_on_server = find_photo(img_number) | ||
if filename_on_server == last_filename: | ||
return None | ||
url = f'{cfg.ip}:{cfg.http_port}/{filename_on_server}' | ||
response = urllib3.PoolManager().request("GET", url) | ||
data = response.data if response.status == http.HTTPStatus.OK else None | ||
return data | ||
|
||
|
||
def take_photo(save_path=cfg.camera_save_path, img_number_on_server=0): | ||
last_filename = find_photo(img_number_on_server) | ||
trigger_remote_camera(img_number_on_server) | ||
i, delay, timeout = 0, 0.3, 3 | ||
img_data = download_photo(img_number_on_server, last_filename) | ||
while img_data is None: | ||
if i * delay >= timeout: | ||
print('failed to take photo') | ||
return False | ||
i += 1 | ||
time.sleep(delay) | ||
img_data = download_photo(img_number_on_server, last_filename) | ||
with open(save_path, 'wb') as f: | ||
f.write(img_data) | ||
return True | ||
|
||
@contextmanager | ||
def measure_time(): | ||
t0 = None | ||
try: | ||
t0 = datetime.now() | ||
yield | ||
finally: | ||
t1 = datetime.now() | ||
if t0 is not None: | ||
delta = t1 - t0 | ||
print(f'execution time: {delta.total_seconds()}') | ||
|
||
|
||
if __name__ == '__main__': | ||
filename = cfg.camera_save_path | ||
with measure_time(): | ||
take_photo(filename) | ||
img = np.rot90(cv2.imread(filename)) | ||
cv_utils.display_img(img, scale=10) | ||
|
||
|
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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