def do_POST(self): formData = cgi.FieldStorage(self.rfile, headers=self.headers, environ={'REQUEST_METHOD': 'POST'}) content = formData.getvalue('content') img = Image.open(BytesIO(content)) base_name = str(int(time.time())) image_name = base_name + '.bmp' img.save(image_name) ocr_image = ocr.ocr(image_name, base_name) ocr_name = base_name + '.png' Image.fromarray(ocr_image).save(ocr_name) blob = storage.upload(CONFIG.project_id, CONFIG.buket, ocr_name, service_account_file='service_account.json') signed_url = storage.signed_url_for_png(blob) shutil.os.remove(image_name) shutil.os.remove(base_name + '.tsv') shutil.os.remove(ocr_name) self.send_response(HTTPStatus.OK) self.send_header('Content-type', 'application/json') self.end_headers() responseBody = json.dumps({'url': signed_url}) self.wfile.write(responseBody.encode('utf-8'))
def gen(df): for idx, row in df.iterrows(): if row['code'] == 66: (image_name, image_file) = get_img(row, FLAGS.apikey) result = upload('trav.finsyn.se', f'tracks/{image_name}.jpg', image_file) print(result)
def process_audio_and_upload(url: str): with tempfile.NamedTemporaryFile( suffix='.mp3') as source_file, tempfile.NamedTemporaryFile( suffix='.wav') as output_file: download_from_url(url, source_file.name) modify_audio(source_file.name, output_file.name) return storage.upload(output_file.name)
def handle(request): """Responds to any HTTP request. Args: request (flask.Request): HTTP request object. Returns: The response text or any set of values that can be turned into a Response object using `make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. """ if request.method == 'OPTIONS': # Allows GET requests from any origin with the Content-Type # header and caches preflight response for an 3600s headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Max-Age': '3600' } return '', 204, headers execution_id = request.headers.get("Function-Execution-Id") # Set CORS headers for the main request headers = {'Access-Control-Allow-Origin': '*'} request_json = request.get_json() image = request_json.get('image') image_bytes = base64.b64decode(image.encode()) storage.upload(image_bytes, execution_id) predictions = get_predictions(image, execution_id) log_predictions(predictions) coloured = identify_colours_with_kmeans(image_bytes, predictions) found_set = find_set(coloured) response = json.dumps(found_set) return response, 200, headers
def hello(): voter_id = request.cookies.get('voter_id') if not voter_id: voter_id = hex(random.getrandbits(64))[2:-1] vote = None if request.method == 'POST': vote = request.form['vote'] data = json.dumps({'voter_id': voter_id, 'vote': vote}) # redis.rpush('votes', data) storage.upload(data) resp = make_response( render_template( 'index.html', option_a=option_a, option_b=option_b, hostname=hostname, vote=vote, )) resp.set_cookie('voter_id', voter_id) return resp
button = gpiozero.Button(17) ready_message = "[PiCeDoFi-IMAPI-RU] system ready" try: audio.play_mp3("startup_comment.mp3") os.system('clear') print(ready_message) while True: if button.is_pressed: print("starting preview...") camera.preview(button) camera.capture(image_file) print('working...') audio.play_mp3("intro_comment.mp3") # upload picture to S3 s3 = storage.upload(image_file) image = draw.load_image(image_file) # call rekognition apis # Step 1. Are there faces in the image? face_result = rekognition.detect_faces_api(s3) face_labels = rekognition.get_face_labels(face_result, ct) if face_labels: os.system('clear') draw.display_text(face_labels) database.inc(face_labels) audio.play_mp3("faces_comment.mp3") polly.speak(face_labels) draw.annotate_faces(image, face_result) # Step 2. Is the picture of a celebrity? celeb_result = rekognition.detect_celebrities_api(s3) celeb_labels = rekognition.get_celebrity_labels(
print("system ready...") while True: # speaker will emit a ready signal when the system is initialized # init celeb_result = None celeb_labels = [] # object is triggered by a mechanical action - button push if button.is_pressed: print("working...") # speaker emits a countdown # capture picture from camera camera = picamera.PiCamera() camera.capture(image_file) # upload picture to S3 s3 = storage.upload(image_file) # call rekognition apis # Step 1. Are there faces in the image? face_result = rekognition.detect_faces_api(s3) face_labels = rekognition.get_face_labels(face_result, ct) if face_labels: database.inc(face_labels) # Step 2. Is the picture of a celebrity? celeb_result = rekognition.detect_celebrities_api(s3) celeb_labels = rekognition.get_celebrity_labels(celeb_result, ct) if celeb_labels: database.inc(celeb_labels) # Step 3. Are there words? text_result = rekognition.detect_text_api(s3) text_labels = rekognition.get_text_labels(text_result, ct) # Step 4. What else is in the picture?
from servicebus import enqueue from storage import delete, upload delete('file1.txt') delete('file2.txt') delete('file3.txt') upload('./sample_file.txt', 'file1.txt') upload('./sample_file.txt', 'file2.txt') upload('./sample_file.txt', 'file3.txt') data1 = '{"timestamp": "2019-09-25 00:00:00.000000", "filename": "file1.txt", "device_id": "device1"}' data2 = '{"timestamp": "2019-09-25 00:00:00.000000", "filename": "file2.txt", "device_id": "device2"}' data3 = '{"timestamp": "2019-09-25 00:00:00.000000", "filename": "file3.txt", "device_id": "device3"}' enqueue(data1, 'SERVICE_BUS_INPUT_QUEUE') enqueue(data2, 'SERVICE_BUS_INPUT_QUEUE') enqueue(data3, 'SERVICE_BUS_INPUT_QUEUE')
def checkFingerprint(self): while True: try: print('mode: ' + self.mode) print('Waiting for finger...') self.lcdControl.lcd_clear() self.lcdControl.lcd_display_string("mode: " + self.mode, 1) self.lcdControl.lcd_display_string("Waiting Finger", 2) while self.f.readImage() == False and self.mode == 'check': pass if self.mode != 'check': return self.f.convertImage(0x01) print('Remove finger...') self.lcdControl.lcd_clear() self.lcdControl.lcd_display_string("mode: " + self.mode, 1) self.lcdControl.lcd_display_string("Remove Finger...", 2) time.sleep(2) result = self.f.searchTemplate() positionNumber = result[0] os.killpg(os.getpgid(self.p.pid), signal.SIGKILL) with PiCamera() as camera: camera.start_preview() time.sleep(1) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview() self.var = self.rand() if storage.download('doorlock/' + self.var + '.jpg'): self.var = self.rand() storage.upload('/home/pi/Desktop/image.jpg', 'doorlock/' + self.var + '.jpg') self.camera() if (positionNumber >= 0): print('Success #' + str(positionNumber)) self.lcdControl.lcd_clear() self.lcdControl.lcd_display_string("mode: " + self.mode, 1) self.lcdControl.lcd_display_string( "Success #" + str(positionNumber), 2) GPIO.output(21, True) time.sleep(1) GPIO.output(21, False) now = datetime.datetime.now() timenow = now.strftime('%Y-%m-%d %H:%M:%S') picturePath = storage.path('doorlock/' + self.var + '.jpg') doorLockstatus = '정상' self.sc.sndMsg('doorlock\timage\t' + str(picturePath[3:]) + '\t' + timenow + '\t' + doorLockstatus) else: print('Failed to FingerPrint') self.lcdControl.lcd_clear() self.lcdControl.lcd_display_string("mode: " + self.mode, 1) self.lcdControl.lcd_display_string("Falied Finger", 2) time.sleep(1) now = datetime.datetime.now() timenow = now.strftime('%Y-%m-%d %H:%M:%S') picturePath = storage.path('doorlock/' + self.var + '.jpg') doorLockstatus = '비정상' self.sc.sndMsg('doorlock\timage\t' + str(picturePath[3:]) + '\t' + timenow + '\t' + doorLockstatus) except Exception as e: print('Operation failed!') print('Exception message: ' + str(e))