def final_steps(xml_total, proxy_total): if int(xml_total) > 0: xml_c.create_xml(xml_total) if int(proxy_total) > 0: gp.get_proxy(proxy_total) complete_msg = f"{'='*25} SCRIPT COMPLETE {'='*25}" logger.info(complete_msg)
def upload_image(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return redirect(url_for("root")) file = request.files['file'] ip = request.remote_addr ###################################### # get the value of multi_label_open from front page # multi_label_open = request.files['file'] # False : indicate return multi label # True : indicate return '1' default is True ###################################### # False : indicate return multi label # True : indicate return '1' default is True signal = str(request.form['identified']) prediction_result = '' top3_dic_names = {} # if user does not select file, browser also submit a empty part without filename if file.filename == '': return redirect(url_for("root")) if file and allowed_file(file.filename): filestr = request.files['file'].read() logging.info( 'From ' + ip + ' -> Upload image ' + file.filename + ' with size of {} kb'.format(len(filestr) / float(1000.0))) try: # convert string data to numpy array npimg = numpy.fromstring(filestr, numpy.uint8) # convert numpy array to image img = cv2.imdecode(npimg, cv2.IMREAD_COLOR) imagename = secure_filename(file.filename).lower() # '7.jpeg' imageshape = img.shape # (1440, 2560, 3) except: print('Corrupt Image:', file.filename) logging.info('Corrupt Image:' + file.filename) return 'error' # filename : is image path such as 'static/img_pool/7.jpeg' # filename = os.path.join("static/img_pool", secure_filename(file.filename).lower()) start = time.clock() executor = APPEntity() if signal == '0': all_coordinates = executor.predict_obj(img) margin = 15 for arry in all_coordinates: arry[-4] -= margin arry[-3] -= margin arry[-2] += margin arry[-1] += margin '''create xml''' prediction_result = create_xml.create_xml( imagename, imageshape, all_coordinates, {}) elif signal == '1': all_coordinates = executor.predict_obj(img) if len(all_coordinates) != 0: margin = 10 for arry in all_coordinates: arry[-4] -= margin arry[-3] -= margin arry[-2] += margin arry[-1] += margin all_coordinates, top3_dic_names = executor.predict_face( img, all_coordinates) prediction_result = create_xml.create_xml( imagename, imageshape, all_coordinates, top3_dic_names) elif signal == '2': all_coordinates = json.loads(request.form.get('coordinates')) all_coordinates_trans = [] if len(all_coordinates) != 0: for arry in all_coordinates: arry_trans = [ arry['Id'], arry['X'], arry['Y'], arry['X'] + arry['W'], arry['Y'] + arry['H'] ] all_coordinates_trans.append(arry_trans) all_coordinates, top3_dic_names = executor.predreict_back( img, all_coordinates_trans) prediction_result = create_xml.create_xml(imagename, imageshape, all_coordinates, top3_dic_names, need_id=True) elif signal == '3': _, all_coordinates_face = demo.demo(sess, net, img) top3_dic_names_face = {} if len(all_coordinates_face) != 0: margin = 10 for arry in all_coordinates_face: arry[-4] -= margin arry[-3] -= margin arry[-2] += margin arry[-1] += margin all_coordinates_face, top3_dic_names_face = executor.predict_face( img, all_coordinates_face) prediction_result_face = create_xml.create_xml( imagename, imageshape, all_coordinates_face, top3_dic_names_face) all_coordinates = json.loads(request.form.get('coordinates')) all_coordinates_trans = [] top3_dic_names_back = {} all_coordinates_back = [] if len(all_coordinates) != 0: for arry in all_coordinates: arry_trans = [ arry['Id'], arry['X'], arry['Y'], arry['X'] + arry['W'], arry['Y'] + arry['H'] ] all_coordinates_trans.append(arry_trans) all_coordinates_back, top3_dic_names_back = executor.predict_back( img, all_coordinates_trans) prediction_result_back = create_xml.create_xml( imagename, imageshape, all_coordinates_back, top3_dic_names_back, need_id=True) prediction_result = prediction_result_face + prediction_result_back elif signal == '4': recognized_class, score = executor.predict_antnet(img) prediction_result = recognized_class + "," + str(score) end = time.clock() logging.info('From ' + ip + ' -> Deal with ' + file.filename + ' using GPU : {} s'.format(end - start)) # return render_template("index.html", prediction_result=prediction_result) return prediction_result return redirect(url_for("root"))
while True: if add_flag: cv2.addWeighted(temp, 0.7, img, 0.3, 0, img) if copy_flag: temp = img.copy() cv2.putText(img, put_text, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2) cv2.imshow('image', img) key = cv2.waitKey(50) & 0xFF if key == ord('q') or key == 27: break elif key == ord('6'): # next image item += 1 if len(text_class) > 0: create_xml(image, nWidth, nHeigth, text_class, positions, xml_file) print('%s file saved to ---> %s file' % (image, xml_name)) add_flag = False copy_flag = False text_class = [] positions = [] break elif key == ord('4'): # former image former_flag = 1 break # else: # break if key == ord('q') or (key & 0xff) == 27: break
def split_resized_img(): ''' data_intput_path imgs & xmls imgs_output_path imgs xmls_output_path xmls ''' print('begin split resized imgs') data_intput_path = os.path.join(data_path, data_type + '_resized_2000_imgs') imgs_output_path = os.path.join(data_path, data_type + '_splited_1200_imgs') xmls_output_path = os.path.join(data_path, data_type + '_splited_1200_xmls') W_pieces = 2 H_pieces = 10 new_size = splited_width if not os.path.exists(imgs_output_path): os.makedirs(imgs_output_path) if not os.path.exists(xmls_output_path): os.makedirs(xmls_output_path) imgs = [s for s in os.listdir(data_intput_path) if s[-4:] == '.jpg'] xmls = [s for s in os.listdir(data_intput_path) if s[-4:] == '.xml'] assert len(imgs) == len(xmls) print(len(imgs)) for i in range(len(imgs)): assert imgs[i][:-4] == xmls[i][:-4] img_path = os.path.join(data_intput_path, imgs[i]) xml_path = os.path.join(data_intput_path, xmls[i]) tree = ET.parse(xml_path) root = tree.getroot() size = get_and_check(root, 'size', 1) width = int(get_and_check(size, 'width', 1).text) height = int(get_and_check(size, 'height', 1).text) H_overlap = int((H_pieces * new_size - height) / (H_pieces - 1)) W_overlap = int((W_pieces * new_size - width) / (W_pieces - 1)) all_splited_imgs = [] for w in range(W_pieces): for h in range(H_pieces): #print(str(w)+" "+str(h)) file_name = xmls[i][:-4] + '_' + str(w) + '_' + str(h) #print(file_name) start_w = np.maximum(w * (new_size - W_overlap), 0) end_w = np.minimum(start_w + new_size, width) start_h = np.maximum(h * (new_size - H_overlap), 0) end_h = np.minimum(start_h + new_size, height) source_img = cv2.imread(img_path) if (source_img.shape[0] >= source_img.shape[1]): splited_img = source_img[start_h:end_h, start_w:end_w, :] else: splited_img = source_img[start_w:end_w, start_h:end_h, :] img_details = {'img_data':splited_img, 'height':new_size, 'width':new_size, \ 'bboxes':[], 'filename':file_name} has_box_iou_too_small = False for obj in get(root, 'object'): category = get_and_check(obj, 'name', 1).text category = category[:2] if int(category) in commom_flaws: bndbox = get_and_check(obj, 'bndbox', 1) xmin = int(get_and_check(bndbox, 'xmin', 1).text) ymin = int(get_and_check(bndbox, 'ymin', 1).text) xmax = int(get_and_check(bndbox, 'xmax', 1).text) ymax = int(get_and_check(bndbox, 'ymax', 1).text) assert (xmax > xmin) assert (ymax > ymin) if ymin < start_h and ymax > end_h: ymin = start_h ymax = end_h if xmin < start_w and xmax > end_w: xmin = start_w xmax = end_w ## bbox in splited img / bbox in raw img IoU = iou([start_w, start_h, end_w, end_h], [xmin, ymin, xmax, ymax]) if IoU < 0.8: has_box_iou_too_small = True break xmin = xmin - start_w ymin = ymin - start_h xmax = xmax - start_w ymax = ymax - start_h img_details['bboxes'].append({ 'class': category, 'x1': xmin, 'y1': ymin, 'x2': xmax, 'y2': ymax }) if (has_box_iou_too_small): continue if (len(img_details['bboxes']) > 0): all_splited_imgs.append(img_details) else: continue if (len(all_splited_imgs) > 3): np.random.shuffle(all_splited_imgs) all_splited_imgs = all_splited_imgs[:3] for i in range(len(all_splited_imgs)): img_detail = all_splited_imgs[i] splited_img_data = img_detail['img_data'] create_xml(img_detail, xmls_output_path) ''' xml_path = os.path.join(xmls_output_path, img_detail['filename']+'.xml') tree = ET.parse(xml_path) root = tree.getroot() for obj in get(root, 'object'): category = get_and_check(obj, 'name', 1).text category = category[:2] assert (int(category) > 60) & (int(category) < 90) bndbox = get_and_check(obj, 'bndbox', 1) xmin = int(int(get_and_check(bndbox, 'xmin', 1).text)) ymin = int(int(get_and_check(bndbox, 'ymin', 1).text)) xmax = int(int(get_and_check(bndbox, 'xmax', 1).text)) ymax = int(int(get_and_check(bndbox, 'ymax', 1).text)) assert(xmax > xmin) assert(ymax > ymin) cv2.rectangle(splited_img_data,(xmin, ymin), (xmax, ymax), (0,255,255),1) textLabel = category textOrg = (xmin, ymin) cv2.putText(splited_img_data, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1) ''' Image.fromarray(splited_img_data).save(imgs_output_path + sep + img_detail['filename'] + '.jpg') print('split resized imgs end')
def resize_raw_imgs(): ''' new_W 图片进行缩小后的宽度,高度同比缩小 data_input_path imgs & xmls data_output_path imgs & xmls ''' print('begin resize raw imgs') new_W = resized_width data_input_path = os.path.join(data_path, data_type + '_raw_imgs') data_output_path = os.path.join(data_path, data_type + '_resized_2000_imgs') if not os.path.exists(data_output_path): os.makedirs(data_output_path) imgs = [s for s in os.listdir(data_input_path) if s[-4:] == '.jpg'] xmls = [s for s in os.listdir(data_input_path) if s[-4:] == '.xml'] assert len(imgs) == len(xmls) print(data_input_path + " " + str(len(imgs))) for i in range(len(imgs)): assert imgs[i][:-4] == xmls[i][:-4] img_path = os.path.join(data_input_path, imgs[i]) xml_path = os.path.join(data_input_path, xmls[i]) ## resize img tree = ET.parse(xml_path) root = tree.getroot() size = get_and_check(root, 'size', 1) width = int(get_and_check(size, 'width', 1).text) height = int(get_and_check(size, 'height', 1).text) if width <= 0 | height <= 0: continue ratio = float(new_W) / width new_H = int(height * ratio) raw_img = cv2.imread(img_path) resized_img = cv2.resize(raw_img, (new_W, new_H)) img_details = {'height':new_H, 'width':new_W, \ 'bboxes':[], 'filename':xmls[i][:-4]} ## resize xml for obj in get(root, 'object'): category = get_and_check(obj, 'name', 1).text category = category[:2] assert (int(category) > 60) & (int(category) < 90) bndbox = get_and_check(obj, 'bndbox', 1) xmin = int(int(get_and_check(bndbox, 'xmin', 1).text) * ratio) ymin = int(int(get_and_check(bndbox, 'ymin', 1).text) * ratio) xmax = int(int(get_and_check(bndbox, 'xmax', 1).text) * ratio) ymax = int(int(get_and_check(bndbox, 'ymax', 1).text) * ratio) assert (xmax > xmin) assert (ymax > ymin) ''' cv2.rectangle(raw_img,(xmin, ymin), (xmax, ymax), (0,0,255),2) textLabel = category textOrg = (xmin, ymin) cv2.putText(raw_img, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1) Image.fromarray(raw_img).save(data_output_path+sep+imgs[i]) ''' img_details['bboxes'].append({ 'class': category, 'x1': xmin, 'y1': ymin, 'x2': xmax, 'y2': ymax }) create_xml(img_details, data_output_path) ''' xml_path = os.path.join(val_resized_imgs, xmls[i]) tree = ET.parse(xml_path) root = tree.getroot() for obj in get(root, 'object'): category = get_and_check(obj, 'name', 1).text category = category[:2] assert (int(category) > 60) & (int(category) < 90) bndbox = get_and_check(obj, 'bndbox', 1) xmin = int(int(get_and_check(bndbox, 'xmin', 1).text)) ymin = int(int(get_and_check(bndbox, 'ymin', 1).text)) xmax = int(int(get_and_check(bndbox, 'xmax', 1).text)) ymax = int(int(get_and_check(bndbox, 'ymax', 1).text)) assert(xmax > xmin) assert(ymax > ymin) cv2.rectangle(resized_img,(xmin, ymin), (xmax, ymax), (0,0,255),2) textLabel = category textOrg = (xmin, ymin) cv2.putText(resized_img, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1) ''' Image.fromarray(resized_img).save(data_output_path + sep + imgs[i]) print('resize raw imgs end')
"").replace(";", "") values = values.replace("(", "").replace(")", "") values = values.replace("&", "").split(",") pin_map[prev] += [ pin.strip() for pin in values if pin.strip() != "" ] """ # Show Port-to-Pin Mappings for k in pin_map: print(k, pin_map[k]) """ with open(lbr_template, 'r') as f: t = f.readlines() template = "".join(t) # Find name of device matches = re.findall(r'(\bentity\b\ +\w+\b is\b)', s, re.IGNORECASE) device = matches[0][6:-3].strip() # Sort and create HTML l_grp, r_grp, l_count, r_count = sort_ports(groups) lbr = cx.create_xml(l_grp, r_grp, l_count, r_count, device, template, pin_map) # Write out to file lbr_nm = "bsdl_files/" + device + ".lbr" lbr_file = open(lbr_nm, 'w') lbr_file.write(lbr) lbr_file.close()