def index(req, resp): # parse query string req.parse_qs() flash = req.form.get('flash', 'false') if flash == 'true': led.on() # Camera resilience - if we fail to init try to deinit and init again if (not camera.init()): camera.deinit() await asyncio.sleep(1) # If we fail to init, return a 503 if (not camera.init()): yield from picoweb.start_response(resp, status=503) yield from resp.awrite('ERROR: Failed to initialise camera\r\n\r\n') return # wait for sensor to start and focus before capturing image await asyncio.sleep(2) buf = camera.capture() led.off() camera.deinit() if type(buf) is bytes and len(buf) > 0: #yield from picoweb.start_response(resp, "image/jpeg") yield from resp.awrite(buf) else: picoweb.http_error(resp, 503)
def capture4(): os.chdir('webserver') camera.init(1) for i in range(100): f = open('frame'+str(i)+'.jpg', 'w') buf = camera.capture() f.write(buf) camera.deinit() f.close()
def envia_foto(Bot, mensaje=''): global camara #------------------ejemplo envio de foto------------------------------------------------------- # camera.init(0, format=camera.JPEG) try: camera.init(0, format=camera.JPEG) except OSError as exc: camera.deinit() Bot.send_message(Bot.canal, exc.args[0]) print(exc.args) return Led = Pin(4, Pin.OUT) # define flash Led.on() #enciende flash # girar vertical camera.flip(0) # giro horizontal camera.mirror(0) camera.framesize(camara.tamanyo_imagen) #'FRAME_240X240'/4, 'FRAME_96X96'/0, 'FRAME_CIF'/6, 'FRAME_FHD'/14, 'FRAME_HD'/11, 'FRAME_HQVGA'/3, 'FRAME_HVGA'/7, 'FRAME_P_3MP'/16, 'FRAME_P_FHD'/20, 'FRAME_P_HD'/15, 'FRAME_QCIF'/2, 'FRAME_QHD'/18, 'FRAME_QQVGA'/1, 'FRAME_QSXGA'/21, 'FRAME_QVGA'/5, 'FRAME_QXGA'/17, 'FRAME_SVGA'/9, 'FRAME_SXGA'/12, 'FRAME_UXGA'/13, 'FRAME_VGA'/8, 'FRAME_WQXGA'/19, 'FRAME_XGA'/10 # efectos especiales camera.speffect(camara.efecto_especial) # EFFECT_NONE (default) EFFECT_NEG EFFECT_BW EFFECT_RED EFFECT_GREEN EFFECT_BLUE EFFECT_RETRO # white balance camera.whitebalance(camara.balance_blancos) # The options are the following: # WB_NONE (default) WB_SUNNY WB_CLOUDY WB_OFFICE WB_HOME # saturation camera.saturation(camara.saturacion) # -2,2 (default 0). -2 grayscale # brightness camera.brightness(camara.brillo) # -2,2 (default 0). 2 brightness # contrast camera.contrast(camara.contraste) #-2,2 (default 0). 2 highcontrast # quality camera.quality(camara.calidad) # 10-63 lower number means higher quality bufer = camera.capture() print('captura realizada') Led.off() #apaga flash with open('file.jpg', 'w') as k: k.write(bufer) bufer = None camera.deinit() Bot.envia_archivo_multipart(configuracion.Chat_Id, 'file.jpg', 'sendPhoto', 'photo', mensaje)
def run(tfile): camera.init() led.on() time.sleep(0.5) buf = camera.capture() led.off() camera.deinit() print(buf[0:8]) f = open(tfile, 'wb') f.write(buf) f.close()
def test(): begin = utime.ticks_ms() time = utime.ticks_ms() camera.init(1) i = 0 while (time - begin < 3000): buf = camera.capture() i = i + 1 time = utime.ticks_ms() camera.deinit() print('Took', i, ' pictures')
def capture3(): os.chdir('webserver') f = open('data.jpg', 'w') camera.init(1) buf = camera.capture() f.write(buf) f = open('data'+str(1)'+.jpg', 'w') buf = camera.capture() f.write(buf) camera.deinit() f.close()
def testThread(): begin = utime.ticks_ms() time = utime.ticks_ms() camera.init(1) i = 0 while (time - begin < 3000): if (n < 5): _thread.start_new_thread(cap, ()) else: pass i = i + 1 time = utime.ticks_ms() machine.sleep(500) camera.deinit() print('Took', i, ' pictures')
def open(): """ Open the camera """ if Camera.opened == False: for i in range(10): res = camera.init() if res == False: print("Camera not initialized") camera.deinit() time.sleep(0.5) else: break else: print("Failed to initialized camera reboot") machine.reset() # Photo on 800x600, motion detection / 8 (100x75), each square detection 8x8 (12.5 x 9.375) Camera.opened = True
def capture2s(): os.chdir('webserver') camera.init(1) begin = utime.ticks_ms() time = begin prev = time i=0 machine.sleep(500) #while(time - begin < 2000): #if(time - prev > -1): prev = time i=i+1 name = 'frame'+str(i)+'.jpg' print(name) f = open(name, 'w') buf = camera.capture() f.write(buf) f.close() #time = utime.ticks_ms() camera.deinit()
def index(req, resp): # parse query string req.parse_qs() flash = req.form.get('flash', 'false') if flash == 'true': led.on() camera.init() # wait for sensor to start and focus before capturing image await asyncio.sleep(2) buf = camera.capture() led.off() camera.deinit() if len(buf) > 0: yield from picoweb.start_response(resp, "image/jpeg") yield from resp.awrite(buf) else: picoweb.http_error(resp, 503)
def main(): # urlは環境に合わせて適宜変更 url = 'http://192.168.0.2:8080/save' errr_count = 0 camera.deinit() try: print('カメラ初期化中...') camera.init() print('カメラ初期化完了') utime.sleep(1) except: print('カメラの初期化に失敗しました。5秒後に再起動します。') utime.sleep(5) machine.reset() while True: try: print('撮影中...') buf = camera.capture() print('撮影完了...') print('画像変換中...') img_byte = base64.b64encode(buf) utime.sleep(2) img_json = ujson.dumps({"image": img_byte}) utime.sleep(2) print('サーバーへ送信中...') res = urequests.post(url, data=img_json) print(res.text) utime.sleep(2) except: errr_count += 1 print(errr_count) if errr_count == 10: # machine.reset() break
wc += 1 if wc >= 5: break return cr def light(state): if flash_enabled == True: if state == '1': flash_light.on() else: flash_light.off() def flash_state(state): flash_enabled = state return flash_enabled def state(): return cr camera.deinit() flash_enabled = False cr = False gc.collect() flash_light = Pin(04, Pin.OUT) flash = False #start()
def index(req, resp): # parse query string req.parse_qs() flash = req.form.get('flash', 'false') if flash == 'true': led.on() stream = req.form.get('stream', 'false') stream = True if stream == 'true' else False # Camera resilience - if we fail to init try to deinit and init again if app_config['camera'] == 'ESP32-CAM': if (not camera.init(0, format=camera.JPEG)): camera.deinit() await asyncio.sleep(1) # If we fail to init, return a 503 if (not camera.init(0, format=camera.JPEG)): yield from picoweb.start_response(resp, status=503) yield from resp.awrite('ERROR: Failed to initialise camera\r\n\r\n') return elif app_config['camera'] == 'M5CAMERA': if (not camera.init(0, d0=32, d1=35, d2=34, d3=5, d4=39, d5=18, d6=36, d7=19, href=26, vsync=25, reset=15, sioc=23, siod=22, xclk=27, pclk=21)): camera.deinit() await asyncio.sleep(1) # If we fail to init, return a 503 if (not camera.init(0, d0=32, d1=35, d2=34, d3=5, d4=39, d5=18, d6=36, d7=19, href=26, vsync=25, reset=15, sioc=23, siod=22, xclk=27, pclk=21)): #M5CAMERA yield from picoweb.start_response(resp, status=503) yield from resp.awrite('ERROR: Failed to initialise camera\r\n\r\n') return # wait for sensor to start and focus before capturing image await asyncio.sleep(2) n_frame = 0 while True: n_try = 0 buf = False while (n_try < 10 and buf == False): #{ # wait for sensor to start and focus before capturing image buf = camera.capture() if (buf == False): await asyncio.sleep(2) n_try = n_try + 1 if (not stream): led.off() camera.deinit() if (type(buf) is bytes and len(buf) > 0): try: if (not stream): yield from picoweb.start_response(resp, "image/jpeg") yield from resp.awrite(buf) print('JPEG: Output frame') break if (n_frame == 0): yield from picoweb.start_response(resp, "multipart/x-mixed-replace; boundary=myboundary") yield from resp.awrite('--myboundary\r\n') yield from resp.awrite('Content-Type: image/jpeg\r\n') yield from resp.awrite('Content-length: ' + str(len(buf)) + '\r\n\r\n') yield from resp.awrite(buf) except: # Connection gone? print('Connection closed by client') led.off() camera.deinit() return else: if (stream): led.off() camera.deinit() #picoweb.http_error(resp, 503) yield from picoweb.start_response(resp, status=503) if (stream and n_frame > 0): yield from resp.awrite('Content-Type: text/html; charset=utf-8\r\n\r\n') yield from resp.awrite('Issues:\r\n\r\n' + str(buf)) return print('MJPEG: Output frame ' + str(n_frame)) n_frame = n_frame + 1
def close(): """ Close the camera """ if Camera.opened == True: camera.deinit() Camera.opened = False