def index(): dataset = MNIST(config.DATA_DIR, train=False) images = [] for i in range(0, min(int(dataset.data.shape[0]), 300)): images.append( (i, image_to_base64(get_pil_image_from_dataset(dataset, i)))) return render_template('index.html', sample_images=images)
def read_img(inputQ): print('Process to write: %s' % os.getpid()) # 读取一帧图像 cap = cv2.VideoCapture(0) while (True): rval, frame = cap.read() if rval: # cv2.imshow("test2", frame) # start_time = time.time() # 转换为base64 编码 b64_code = image_to_base64(frame) # 将编码后的图像放入输入队列 inputQ.append(b64_code)
def save_html(dsobjects, nick, colors, tmp_path): ''' Output a series of HTML pages from the title, pictures, and descriptions ''' htmlcode = '' if len(dsobjects) == 0: return None for i, dsobj in enumerate(dsobjects): htmlcode += HTML_GLUE['slide'][0] + str(i) htmlcode += HTML_GLUE['slide'][1] + \ HTML_GLUE['div'][0] if 'title' in dsobj.metadata: htmlcode += HTML_GLUE['h1'][0] + \ dsobj.metadata['title'] + \ HTML_GLUE['h1'][1] pixbuf = None media_object = False try: pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( dsobj.file_path, 800, 600) key = 'img2' except: pixbuf = get_pixbuf_from_journal(dsobj, 300, 225) key = 'img' if pixbuf is not None: tmp = HTML_GLUE[key][0] tmp += image_to_base64(pixbuf, tmp_path) tmp += HTML_GLUE[key][1] if 'description' in dsobj.metadata: tmp += '<p class="body">' + dsobj.metadata['description'] + '</p>' htmlcode += tmp + \ HTML_GLUE['div'][1] return HTML_GLUE['doctype'] + \ HTML_GLUE['html'][0] + \ HTML_GLUE['head'][0] + \ HTML_GLUE['meta'] + \ HTML_GLUE['title'][0] + \ nick + ' ' + _('Portfolio') + \ HTML_GLUE['title'][1] + \ '<style type="text/css">\n<!--\n-->\nbody {background-color:' + colors[0] + ';}\np.head {font-size: 18pt; font-weight: bold; font-family: "Sans"; }\np.body {font-size: 12pt; font-weight: regular; font-family: "Sans"; }\ndiv.box{width:630px; padding:10px; border:5px; margin:7px; background:' + colors[1] + '}\n</style>\n' +\ HTML_GLUE['head'][1] + \ HTML_GLUE['body'][0] + \ htmlcode + \ HTML_GLUE['body'][1] + \ HTML_GLUE['html'][1]
def test_baidu_face_api(): img = cv2.imread("img/leijinpeng.jpg") baidu = utils.BaiduAPI() ret = baidu.get_face_cli().face_search(utils.image_to_base64(img), "BASE64", "ahojcn") print(ret)
def predict(self, payload): # Get image from url try: image = url_to_image(payload["image_url"]) except: image = b64_to_image(payload["image_b64"]) orig = image.copy() (h, w) = image.shape[:2] # construct a blob from the image blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the face detections print("[INFO] computing face detections...") self.net.setInput(blob) detections = self.net.forward() # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the detection confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence is # greater than the minimum confidence if confidence > args["confidence"]: # compute the (x, y)-coordinates of the bounding box for # the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # ensure the bounding boxes fall within the dimensions of # the frame (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) # extract the face ROI, convert it from BGR to RGB channel # ordering, resize it to 224x224, and preprocess it face = image[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) face = np.expand_dims(face, axis=0) # pass the face through the model to determine if the face # has a mask or not (mask, withoutMask) = self.model.predict(face)[0] # determine the class label and color we'll use to draw # the bounding box and text label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) # include the probability in the label label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100) # display the label and bounding box rectangle on the output # frame cv2.putText(image, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(image, (startX, startY), (endX, endY), color, 2) return image_to_base64(image)
def inference(): testId = request.args.get('testId', None) if testId is None: return "ERROR: Invalid testId Parameter", 400 dataset = MNIST(config.DATA_DIR, train=False) test_id = int(testId) if test_id < 0 or test_id >= dataset.data.shape[0]: return "ERROR: Out of Range testId Parameter", 400 test_img = image_to_base64(get_pil_image_from_dataset(dataset, test_id)) device = torch.device('cpu') model = MNISTNet() model.load_state_dict( torch.load(join(config.MODEL_DIR, 'mnist_cnn.pt'), map_location=device)) model.eval() normalized_data = MNIST(config.DATA_DIR, train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, )) ])) with torch.no_grad(): data = normalized_data.data[test_id, :, :] data = data.type(torch.float32) data = data.unsqueeze(0).unsqueeze(0) target = normalized_data.targets[test_id] data, target = data.to(device), target.to(device) output = model(data) pred = output.argmax( dim=1, keepdim=True) # get the index of the max log-probability output_val = np.exp(output.numpy()).squeeze() pred_idx = int(pred.numpy()[0][0]) pred_score = output_val[pred_idx] gt_label = dataset.classes[int(dataset.test_labels[test_id])] pred_label = dataset.classes[pred_idx] fig = Figure(figsize=(10, 10), dpi=100) canvas = FigureCanvas(fig) ax = fig.subplots() x = np.arange(0, 10, 1) ax.set_xticks(x) # Set ticks labels for x-axis ax.set_xticklabels(dataset.classes, rotation='vertical', fontsize=18) # ax = fig.add_axes([.1, .1, .8, .8]) ax.plot(output_val) # ax.title('Predictions') inf_img = figure_to_base64(fig) return render_template('inference.html', test_id=test_id, test_img=test_img, inf_img=inf_img, gt_label=gt_label, pred_label=pred_label)
import cv2 from aip import AipFace import utils client = AipFace(utils.BAIDU_AI_APP_ID, utils.BAIDU_AI_API_KEY, utils.BAIDU_AI_SECRET_KEY) image_type = "BASE64" group_id = "ahojcn" user_id = "fancheng" options = {"user_info": "范程"} image = cv2.imread("./img/fancheng.jpg") image_base64 = utils.image_to_base64(image) # print(client.addUser(image_base64, image_type, group_id, user_id, options)) import requests, json # host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={utils.BAIDU_AI_API_KEY}&client_secret={utils.BAIDU_AI_SECRET_KEY}' # response = requests.get(host) # if response: # print(response.json()) access_token = '24.e4265a7b77a8cadbdb194334fa9b5cbf.2592000.1611411456.282335-18145377' request_url = 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add' data = { "image": image_base64, "image_type": "BASE64", "group_id": "ahojcn", "user_id": "fancheng",