async def create_upload_file(file: UploadFile = File(...)): file_name = file.filename file.save('./{}'.format(file_name)) packets = rdpcap('./{}'.format(file_name)) res = {} data = [] # Let's iterate through every packet for packet in packets: res = { 'Ethernet': { 'dst': '{}'.format(packet.sprintf("%Ether.dst%")), 'src': '{}'.format(packet.sprintf("%Ether.src%")), 'type': '{}'.format(packet.sprintf("%Ether.type%")) }, 'ARP': { 'hwtype': '{}'.format(packet.sprintf("%ARP.hwtype%")), 'ptype': '{}'.format(packet.sprintf("%ARP.ptype%")), 'hwlen': '{}'.format(packet.sprintf("%ARP.hwlen%")), 'plen': '{}'.format(packet.sprintf("%ARP.plen%")), 'op': '{}'.format(packet.sprintf("%ARP.op%")), 'hwsrc': '{}'.format(packet.sprintf("%ARP.hwsrc%")), 'psrc': '{}'.format(packet.sprintf("%ARP.psrc%")), 'hwdst': '{}'.format(packet.sprintf("%ARP.hwdst%")), 'pdst': '{}'.format(packet.sprintf("%ARP.pdst%")) }, 'Padding': { 'load': '{}'.format(packet.sprintf("%Padding.load%")) } } data.append(res) return data
def upload_waybillimg(request: Request, waybillNo: str, file: UploadFile = File(...),): referrer = request.headers.get("Referer") print('upload_image()-request.url: %s \t referrer: %s' % (request.url, referrer)) # if 'file' not in file. request.files: if len(file) < 1 : flash('No file part') return RedirectResponse(referrer) # file = request.files['file'] print("file tag", file) if file.filename == '': flash('No image selected for uploading') return RedirectResponse(referrer) default_wbNo = 'waybillNo' # waybillNo = request.form.get('waybillNo', default_wbNo) info = ['NA', 'NA', 'NA', 'NA'] for wb in waybillList: if waybillNo == wb[0]: info = wb if file and allowed_file(file.filename): original_filename = secure_filename(file.filename) file_ext = '.' + (original_filename.rsplit('.', 1)[1].lower().__str__()) print('from referrer %s waybillno %s' % (referrer, waybillNo)) saving_filename = waybillNo + file_ext unique_filename = make_unique(saving_filename) # file.save(os.path.join(UPLOAD_FOLDER, filename)) file.save(os.path.join(UPLOAD_FOLDER, unique_filename)) print(os.path.join(UPLOAD_FOLDER, unique_filename)) # unique_thumbnail = save_thumbnail(os.path.join(UPLOAD_FOLDER, unique_filename)) # print('upload_image filename: ' + filename) flash('Image successfully uploaded and displayed') # if 'waybill_images' in request.url: # # return templates.TemplateResponse('waybillData.html', {'name':waybillNo, 'filename':unique_filename, 'info':info}) # return RedirectResponse(referrer) # else: # return templates.TemplateResponse('upload.html', {'filename':unique_thumbnail}) # # return templates('upload.html', filename=unique_filename) else: flash('Allowed image types are -> png, jpg, jpeg, gif') return RedirectResponse(referrer)
async def create_upload_file(type: str, format: str, background_tasks: BackgroundTasks, model: UploadFile = File(...), dataset: UploadFile = File(default=None)): print("uploading", flush=True) global labels, datapath, instanceReady instanceReady = False if ((format == 'edgetpu' or format == 'tflite_quantized') and dataset == None): return {'No representative dataset supplied'} model_dir = tempfile.mkdtemp() print("### Created " + model_dir) data_dir = tempfile.mkdtemp() unzipFile(model, model_dir) background_tasks.add_task(cleanup_files, model_dir, data_dir) with open(model_dir + '/metadata.json') as json_file: data = json.load(json_file) labels = data['labels'] print("Generating lables.txt") with open(model_dir + '/labels.txt', 'w') as f: for idx, label in enumerate(labels): f.write("{} {}\n".format(idx, label)) print('Labels:' + ', '.join(labels), flush=True) print('converting model to keras', flush=True) os.system( 'tensorflowjs_converter --input_format tfjs_layers_model --output_format keras "' + model_dir + '/model.json" ' + model_dir + '/keras_model.h5') if format == 'keras': return returnFile('keras_model.h5', model_dir, data_dir) # Generate savedmodel print('converting model to saved model', flush=True) model = tf.keras.models.load_model(model_dir + '/keras_model.h5') model.save(model_dir + '/model.savedmodel') if format == 'savedmodel': return returnFile('model.savedmodel', model_dir, data_dir, True) if format == 'tflite': # Generate tflite unquantized tflite_unquant_model = convertSavedModelTFLiteUnQuantized( model_dir + '/model.savedmodel') open(model_dir + '/model_unquant.tflite', 'wb').write(tflite_unquant_model) return returnFile('model_unquant.tflite', model_dir, data_dir) # Generate tflite unzipFile(dataset, data_dir) datapath = data_dir print('convert model to tflite', flush=True) tflite_quant_model = converterSavedModelTFLite(model_dir + '/model.savedmodel') open(model_dir + '/model.tflite', 'wb').write(tflite_quant_model) if format == 'tflite_quantized': return returnFile('model.tflite', model_dir, data_dir) # Generate edgetpu model print('compile model for edgetpu', flush=True) os.system('edgetpu_compiler -s ' + model_dir + '/model.tflite -o ' + model_dir) if format == 'edgetpu': return returnFile('model_edgetpu.tflite', model_dir, data_dir) else: return {'invalid format': format}