Skip to content

Commit

Permalink
use open camera remote to trigger smartphone camera
Browse files Browse the repository at this point in the history
  • Loading branch information
sitzmann committed Feb 1, 2022
1 parent 947d6d3 commit 213b4e4
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
11 changes: 10 additions & 1 deletion cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@
min_contour_area_digit = 20



# drawing
border_size_m = 3
draw_scale = 8.7
disc_size_m = 0.6


# open camera remote server
ip = '192.168.178.22'
http_port = 8080
udp_port = 8000
camera_save_path = input_imgs_dir + 'current.jpg'
img_prefix = 'IMG_'
autofocus_time_seconds = 0 # delay to wait for autofocus


# options
show_digits = False
show_circles = False
Expand Down
2 changes: 1 addition & 1 deletion drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def rel_line(x, y):
ctx.rel_line_to(*m2p([x, y]))


def show(surface, filename='temp', wait=10, pos=4):
def show(surface, filename='result', wait=10, pos=4):
os.makedirs(cfg.media_out_dir, exist_ok=True)
path = f'{cfg.media_out_dir}/{filename}.png'
surface.write_to_png(path)
Expand Down
92 changes: 92 additions & 0 deletions ip_camera.py
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)


2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import scan
import drawer
import cfg
import ip_camera
imgs_dir = 'input_imgs/'


def main():
# for img_path in [imgs_dir + 'ho-stack-1.jpg', imgs_dir + 'ho-stack-2.jpg']:
ip_camera.take_photo()
player_positions = scan.scan(cfg.demo_img)
surface = drawer.draw_scene(player_positions)
drawer.show(surface, wait=0)
Expand Down
Binary file modified media_out/temp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def resize_img(img, show_resized=cfg.show_input):
h, w = img.shape[:2]
scale = h / 1280
img = cv2.resize(img, (int(w / scale), int(h / scale)))
cv_utils.display_img(img, 'input img', wait=False) if show_resized else None
cv_utils.display_img(img, 'input', wait=False) if show_resized else None
return img


Expand Down

0 comments on commit 213b4e4

Please sign in to comment.