Created
February 5, 2017 22:40
-
-
Save albcunha/a18e66dab607083fffdf011b71fc1734 to your computer and use it in GitHub Desktop.
This code takes photo form android app ip_cam from two camera. It was used to with a Samsung S5 and S7 as bookscanner. It worked perfectally. In addition, You can see the video stream from VLC through the "ip + /video'.
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
import requests | |
import grequests | |
import os | |
import time | |
import shutil | |
import os.path | |
from PIL import Image | |
ip_left_camera = 'http://192.168.43.1:8080' | |
ip_right_camera = 'http://192.168.43.1:8080' | |
def get_img_dir(): | |
src_dir = os.path.dirname(__file__) | |
img_dir = os.path.join(src_dir) | |
return img_dir | |
def open_img(img_name): | |
img_dir = get_img_dir() | |
full_img_path = os.path.join(img_dir, img_name) | |
img = Image.open(full_img_path) | |
return img | |
def crop_image(img, xy, scale_factor): | |
'''Crop the image around the tuple xy | |
Inputs: | |
------- | |
img: Image opened with PIL.Image | |
xy: tuple with relative (x,y) position of the center of the cropped image | |
x and y shall be between 0 and 1 | |
scale_factor: the ratio between the original image's size and the cropped image's size | |
''' | |
center = (img.size[0] * xy[0], img.size[1] * xy[1]) | |
new_size = (img.size[0] / scale_factor, img.size[1] / scale_factor) | |
left = max (0, (int) (center[0] - new_size[0] / 2)) | |
right = min (img.size[0], (int) (center[0] + new_size[0] / 2)) | |
upper = max (0, (int) (center[1] - new_size[1] / 2)) | |
lower = min (img.size[1], (int) (center[1] + new_size[1] / 2)) | |
cropped_img = img.crop((left, upper, right, lower)) | |
return cropped_img | |
def save_img(img, img_name): | |
img_dir = get_img_dir() | |
full_img_path = os.path.join(img_dir, img_name) | |
img.save(full_img_path) | |
def executa(): | |
count=0 | |
arquivo = input('Informe o nome do arquivo:') | |
while True: | |
input("Verificando foco do lado esquerdo. Aperte enter para tirar foto") | |
left_photoaf = ip_left_camera + '/photoaf.jpg' | |
r_esquerdo = requests.get(left_photoaf, stream=True) | |
if r_esquerdo.status_code == 200: | |
with open('foco_e.jpg', 'wb') as f: | |
r_esquerdo.raw.decode_content = True | |
shutil.copyfileobj(r_esquerdo.raw, f) | |
ams = open_img('foco_e.jpg') | |
crop_ams = crop_image(ams, (0.5, 0.35), 2.5) | |
crop_ams = crop_ams.rotate(180) | |
crop_ams.show() | |
resposta = input("Foco Esquerdo OK? (S/N) ") | |
if resposta == 'S': | |
break | |
if resposta == 'N': | |
continue | |
else: | |
pass | |
while True: | |
input("Verificando foco do lado direito. Aperte enter para tirar foto") | |
right_photoaf = ip_right_camera + '/photoaf.jpg' | |
r_direito = requests.get(right_photoaf, stream=True) | |
if r_direito.status_code == 200: | |
with open('foco_d.jpg', 'wb') as f: | |
r_direito.raw.decode_content = True | |
shutil.copyfileobj(r_direito.raw, f) | |
ams = open_img('foco_d.jpg') | |
crop_ams = crop_image(ams, (0.5, 0.35), 2.5) | |
crop_ams.show() | |
resposta = input("Foco Direito OK? (S/N) ") | |
if resposta == 'S': | |
break | |
if resposta == 'N': | |
continue | |
else: | |
pass | |
while True: | |
para = input('Aperte Enter para próxima página') | |
print('\n\n') | |
if len(para) > 0: | |
print("Encerrado!") | |
try:os.remove('teste_e.jpg') | |
except:pass | |
try:os.remove('teste_d.jpg') | |
except:pass | |
break | |
left_photo = 'ip_left_camera' + '/photo.jpg' | |
right_photo = 'ip_right_camera' + '/photo/jpg' | |
urls = [left_photo, right_photo] | |
rs = (grequests.get(url) for url in urls) | |
for response in grequests.map(rs): | |
if response.url == left_photo: | |
r_esquerdo = response | |
if r_esquerdo.status_code == 200: | |
with open('teste_e.jpg', 'wb') as f: | |
#r_esquerdo.raw.decode_content = True | |
#shutil.copyfileobj(r_esquerdo.raw, f) | |
f.write(r_esquerdo.content) | |
arquivo_imagem = arquivo + "_" + str(count) + "_E.jpg" | |
try:os.remove(arquivo_imagem) | |
except:pass | |
os.rename('teste_e.jpg',arquivo_imagem) | |
print('Arquivo1 "' + arquivo_imagem + '" salvo.') | |
if response.url == right_photo: | |
r_direito = response | |
if r_direito.status_code == 200: | |
with open('teste_d.jpg', 'wb') as f: | |
#r_direito.raw.decode_content = True | |
#shutil.copyfileobj(r_direito.raw, f) | |
f.write(r_direito.content) | |
arquivo_imagem = arquivo + "_" + str(count) + "_D.jpg" | |
try:os.remove(arquivo_imagem) | |
except:pass | |
os.rename('teste_d.jpg',arquivo_imagem) | |
print('Arquivo2 "' + arquivo_imagem + '" salvo.') | |
count+=1 | |
executa() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment