Ejemplo n.º 1
0
 def merge(self, image_root='test_merge', step=500, set_label=''):
     self.delete_invalid_files(image_root)
     file_list = os.listdir(image_root)
     shuffle(file_list)
     count = 0
     for i in range(len(file_list) // step + 1):
         full_name = image_root + "/" + file_list[i * step]
         temp_image = cv2.imdecode(np.fromfile(full_name, np.uint8), -1)
         temp_label = [self.get_class_label(file_list[i * step])[1]]
         img = None
         for file_path in file_list[i * step + 1: (i + 1) * step]:
             try:
                 full_name = image_root + "/" + file_path
                 img = cv2.imdecode(np.fromfile(full_name, np.uint8), -1)
                 temp_image = np.hstack((temp_image, img))
                 temp_label.append(self.get_class_label(file_path)[1])
                 print("\r已处理%5d张图片, %20s" % (count, file_path), end='')
                 count += 1
             except Exception as e:
                 print(img.shape, file_path)
                 raise e
         print()
         cv2.imencode('.png', temp_image)[1].tofile(set_label + "_images_%d.png" % i)
         np.save(set_label + '_labels_%d.npy' % i, np.array(temp_label))
         print(len(temp_label), temp_image.shape)
Ejemplo n.º 2
0
def predict(update: Update, context: CallbackContext) -> None:
    """Echo the user message."""
    if update.message.text:
        update.message.reply_text(update.message.text)
    message: Message = update.message
    photo: List[PhotoSize] = message.photo
    document: Document = message.document
    context.bot.send_chat_action(chat_id=message.chat.id,
                                 action=ChatAction.UPLOAD_PHOTO,
                                 timeout=60000)
    if photo:
        p: PhotoSize = photo[-1]
        file: File = p.get_file(timeout=10000)
        arr: bytearray = file.download_as_bytearray()
        nparr = np.frombuffer(arr, np.uint8)
        inp_img = cv2.imdecode(np.frombuffer(nparr, np.uint8),
                               cv2.IMREAD_UNCHANGED)
        decode_an_image_file(inp_img)
        output = cv2.imread('h.png')
        _, outputBuffer = cv2.imencode('.jpg', output)
        OutputBase64String = base64.b64encode(outputBuffer).decode('utf-8')
        message.reply_photo(photo=open('h.png', 'rb'))
    elif document:
        file: File = document.get_file(timeout=10000)
        arr: bytearray = file.download_as_bytearray()
        nparr = np.frombuffer(arr, np.uint8)
        inp_img = cv2.imdecode(np.frombuffer(nparr, np.uint8),
                               cv2.IMREAD_UNCHANGED)
        decode_an_image_file(inp_img)
        output = cv2.imread('h.png')
        _, outputBuffer = cv2.imencode('.jpg', output)
        OutputBase64String = base64.b64encode(outputBuffer).decode('utf-8')
        message.reply_photo(photo=open('h.png', 'rb'))
    else:
        pass
Ejemplo n.º 3
0
def setStandardValues(base64Image, values):

	d = []
	for i in values:
		d.append(int(i['value']))
	(h_min, h_max, s_min, s_max, v_min, v_max, threshold1, threshold2, area_min) = tuple(d)

	img_str = base64.b64decode(base64Image)

	nparr = np.fromstring(img_str, np.uint8)
	image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
	imgHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
	lower = np.array([h_min, s_min, v_min])
	upper = np.array([h_max, s_max, v_max])
	mask = cv2.inRange(imgHSV, lower, upper)
	result = cv2.bitwise_and(image, image, mask = mask)
	mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
	imgCanny = cv2.Canny(mask, threshold1, threshold2)
	kernel = np.ones((5,5))
	imgDil = cv2.dilate(imgCanny, kernel, iterations=1)
	found, standardHeight = getContour(imgDil, image)

	img_str = cv2.imencode('.png', imgHSV)[1].tobytes()
	base64ImageReturn = base64.b64encode(img_str) 
	
	imgD_str = cv2.imencode('.png', result)[1].tobytes()
	base64ImgDilReturn = base64.b64encode(imgD_str)

	return base64ImageReturn, base64ImgDilReturn
Ejemplo n.º 4
0
 def resize(self):
     flags = ["\\", "|", "/", "|"]
     for i, file_name in enumerate(os.listdir(self.folder_path)):
         full_name = self.folder_path + file_name
         img = cv2.imdecode(np.fromfile(full_name, dtype=np.uint8), -1)
         save_name = "%s%s_%d.png" % (self.folder_path, self.class_name, i)
         save_file = cv2.resize(img, (400, 400))
         cv2.imencode('.png', save_file)[1].tofile(save_name)
         if save_name != full_name:
             os.remove(full_name)
         print("\r", flags[i % 4], "已格式化%d张图片, 保存为:%s_%d.png" % (i + 1, self.class_name, i), end='')
     print()
Ejemplo n.º 5
0
def handle(conn):
    global img, imbyt
    lock.acquire()
    if imbyt is None:
        imorg = np.asarray(ImageGrab.grab())  # 全屏截图

        # 压缩编码
        _, imbyt = cv2.imencode(".jpg", imorg,
                                [cv2.IMWRITE_JPEG_QUALITY, IMQUALITY])
        imnp = np.asarray(imbyt, np.uint8)
        img = cv2.imdecode(imnp, cv2.IMREAD_COLOR)
    lock.release()
    lenb = struct.pack(">BI", 1, len(imbyt))
    conn.sendall(lenb)
    conn.sendall(imbyt)
    while True:

        cv2.waitKey(100)  # 等100纳秒

        gb = ImageGrab.grab()  # 全屏截图
        imgnpn = np.asarray(gb)
        _, timbyt = cv2.imencode(".jpg", imgnpn,
                                 [cv2.IMWRITE_JPEG_QUALITY, IMQUALITY])
        imnp = np.asarray(timbyt, np.uint8)
        imgnew = cv2.imdecode(imnp, cv2.IMREAD_COLOR)
        # 计算图像差值
        imgs = imgnew - img
        if (imgs != 0).any():
            # 画质改变
            pass
        else:
            continue
        imbyt = timbyt
        img = imgnew
        # 无损压缩

        _, imb = cv2.imencode(".png", imgs)
        l1 = len(imbyt)  # 原图像大小
        l2 = len(imb)  # 差异图像大小
        if l1 > l2:

            # 传差异化图像
            lenb = struct.pack(">BI", 0, l2)
            conn.sendall(lenb)
            conn.sendall(imb)

        else:
            # 传原编码图像
            lenb = struct.pack(">BI", 1, l1)
            conn.sendall(lenb)
            conn.sendall(imbyt)
Ejemplo n.º 6
0
def read_image_and_process(row, detector=None, start_time=None):
    # stampa di stato (se possibile)
    if row.name:
        if row.name % 200 == 1:
            # print(int(row.name)/62000)
            s = "Numero... {}".format(row.name)
            if start_time:
                s += " ~ Tempo totale: {}".format(time.time() - start_time)
            print(s, flush=True)

    image_path = row.path
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    # E ADESSO GENERIAMO I QUADRATI
    if detector:
        # Quadrati generati grazie alla face detection
        detect_results = detector.detect_faces(
            image
        )  # una lista di dizionari, ciascuno contenente box, confidence, keypoints
        #se troviamo nessuna o tante facce
        if len(detect_results) != 1:
            # scartiamo questa riga (delegando lo scarto vero e proprio al resto di make_true_dataset)
            row[AGE_COL] = -190405059
            return row
        box = convert_box_ipazc_to_ours(detect_results[0]["box"])
        keypoints = detect_results[0]["keypoints"]
        crop_boxes = generate_the_three_boundingboxes(box, keypoints)
    else:
        # Quadrati fissi, vicino al centro dell'immagine.
        # molto subottimale, ma è stato utile per i primi prototipi.
        crop_boxes = fixed_squares(image)
    __status, encoded_img = cv2.imencode(".jpg", image)
    row[IMAGE_COL] = encoded_img.tobytes()
    row[CROP_BOXES_COL] = crop_boxes.dumps()

    return row
Ejemplo n.º 7
0
async def ocr(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as r:
            size = int(r.headers['Content-length'])
            if size > 1e6:
                img = np.asarray(bytearray(await r.read()), dtype="uint8")
                flag = cv2.IMREAD_GRAYSCALE
                if size > 2e6:
                    flag = cv2.IMREAD_REDUCED_GRAYSCALE_2
                img = cv2.imdecode(img, flag)
                _, img = cv2.imencode(os.path.splitext(url)[1], img)
                data = aiohttp.FormData()
                data.add_field('apikey', API_KEY)
                data.add_field('OCREngine', '2')
                data.add_field('file',
                               img.tobytes(),
                               content_type='image/png',
                               filename='image.png')
                ocr_url = 'https://api.ocr.space/parse/image'
                async with session.post(ocr_url, data=data) as r:
                    json = await r.json()
            else:
                ocr_url = f'https://api.ocr.space/parse/imageurl?apikey={API_KEY}&OCREngine=2&url={url}'
                async with session.get(ocr_url) as r:
                    json = await r.json()
            if json['OCRExitCode'] != 1:
                return False, '.'.join(json['ErrorMessage'])
            return True, json['ParsedResults'][0]['ParsedText']
Ejemplo n.º 8
0
def generate():
    # grab global references to the output frame and lock variables
    global outputFrame, lock

    # loop over frames from the output stream
    while True:

        # wait until the lock is acquired
        with lock:
            # check if the output frame is available, otherwise skip
            # the iteration of the loop
            if outputFrame is None:
                continue

            # encode the frame in JPEG format
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)

            # ensure the frame was successfully encoded
            if not flag:
                continue

        # yield the output frame in the byte format
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) +
               b'\r\n')
Ejemplo n.º 9
0
def canny_edge_detection_preview():
    """
    edge detection preview
    ---
    tags:
        -   image
    parameters:
        -   in: formData
            name: image
            type: file
            required: true
            description: The image to upload.
    responses:
        200:
            description: the edge detection preview image
            content:
                image/png:
                    schema:
                        type: string
                        format: binary
    """
    if 'image' not in request.files:
        raise ParameterLostError("image")

    img = cv2.imdecode(
        numpy.fromstring(request.files['image'].read(), numpy.uint8),
        cv2.IMREAD_UNCHANGED)

    edges = cv2.Canny(img, 100, 200)

    _, f = cv2.imencode(".png", edges)

    return send_file(io.BytesIO(f.tobytes()), "image/png")
Ejemplo n.º 10
0
def clientSide():
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    global client_address
    print("client-1", client_address)
    host_ip = str(client_address)  # paste your server ip address here
    port = 9999
    client_socket.connect((host_ip, port))  # a tuple
    data = b""
    payload_size = struct.calcsize("Q")
    while True:
        while len(data) < payload_size:
            packet = client_socket.recv(4 * 1024)  # 4K
            if not packet: break
            data += packet
        packed_msg_size = data[:payload_size]
        data = data[payload_size:]
        msg_size = struct.unpack("Q", packed_msg_size)[0]

        while len(data) < msg_size:
            data += client_socket.recv(4 * 1024)
        frame_data = data[:msg_size]
        data = data[msg_size:]
        frame = pickle.loads(frame_data)
        ret, jpeg = cv2.imencode('.jpg', frame)
        frame = jpeg.tobytes()

        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\n' + frame + b'\r\n\r\n')
Ejemplo n.º 11
0
    def get_frame(self):
        success, frame = self.video.read()  # read the camera frame
        font = cv2.FONT_HERSHEY_SIMPLEX
        # Preprocessing Image
        img = np.asarray(frame)
        img = cv2.resize(img, (32, 32))
        img = preprocessing(img)

        img = img.reshape(1, 32, 32, 1)
        cv2.putText(frame, "Class: ", (20, 30), font, 0.6, (0, 0, 255), 2,
                    cv2.LINE_AA)
        cv2.putText(frame, "Probability: ", (20, 70), font, 0.6, (255, 0, 0),
                    2, cv2.LINE_AA)

        # Predict
        predict = model.predict(img)
        classPredict = model.predict_classes(img)

        prob = np.amax(predict)
        print(classNames[classPredict[0]])
        if prob > 0.5:
            cv2.putText(frame, str(classNames[classPredict[0]]), (120, 30),
                        font, 0.6, (0, 0, 255), 2, cv2.LINE_AA)
            cv2.putText(frame,
                        str(round(prob * 100, 2)) + "%", (180, 70), font, 0.6,
                        (255, 0, 0), 2, cv2.LINE_AA)
        ret, image = cv2.imencode('.jpg', frame)
        return image.tobytes()
Ejemplo n.º 12
0
def gen():
    """Video streaming generator function."""

    img = cv2.imread("speed.jpg")
    img = cv2.resize(img, (1280,720), fx=0.5, fy=0.5) 
    frame = cv2.imencode('.jpg', img)[1].tobytes()
    yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def get_image():
    image = request.files["images"]
    image_name = image.filename
    image.save(os.path.join(os.getcwd(), image_name))
    img_raw = tf.image.decode_image(open(image_name, 'rb').read(), channels=3)
    img = tf.expand_dims(img_raw, 0)
    img = transform_images(img, size)

    t1 = time.time()
    boxes, scores, classes, nums = yolo(img)
    t2 = time.time()
    print('time: {}'.format(t2 - t1))

    print('detections:')
    for i in range(nums[0]):
        print('\t{}, {}, {}'.format(class_names[int(classes[0][i])],
                                    np.array(scores[0][i]),
                                    np.array(boxes[0][i])))
    img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR)
    img = draw_outputs(img, (boxes, scores, classes, nums), class_names)
    cv2.imwrite(output_path + 'detection.jpg', img)
    print('output saved to: {}'.format(output_path + 'detection.jpg'))

    # prepare image for response
    _, img_encoded = cv2.imencode('.png', img)
    response = img_encoded.tostring()

    #remove temporary image
    os.remove(image_name)

    try:
        return Response(response=response, status=200, mimetype='image/png')
    except FileNotFoundError:
        abort(404)
Ejemplo n.º 14
0
def generate(rtsp_url):
    # create a variable  to store output frame
    outputFrame = None

    # request video streaming from inputted URL
    vs = VideoStream(src=rtsp_url).start()

    try:
        # loop over frames from the output stream
        while True:
            outputFrame = vs.read()
            if outputFrame is None:
                break

            # encode the frame in JPEG format
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)

            # ensure the frame was successfully encoded
            if not flag:
                continue

            # yield the output frame in the byte format
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' +
                   bytearray(encodedImage) + b'\r\n')

        vs.stop()
    except:
        vs.stop()
        return 'error'
Ejemplo n.º 15
0
    def get_frame():

        camera_port = 0
        cam = cv2.VideoCapture(camera_port)
        password = input('Enter the Gmail password:'******'.jpg', img)[1]
            stringData = imgencode.tobytes()

            yield (b'--frame\r\n'
                   b'Content-Type: Text/plain\r\n\r\n' + stringData + b'\r\n'
                   )  #stream on html page

            process.imgProcess(img, password)  # call to method

        cv2.waitKey(1)
        cam.release()
        cv2.destroyAllWindows()
def test_upscale_frame():
    frame_num = 1
    start = time.time()
    # loop over frames from the video stream
    while True:
        _, frame = camera.read()  # read the camera frame

        frame = cv2.resize(frame, (resize_width, resize_height),
                           interpolation=cv2.INTER_AREA)
        frame = cv2.resize(frame, (target_width, target_height),
                           interpolation=cv2.INTER_AREA)

        end = time.time()
        duration = end - start
        fps = round(frame_num / duration, 1)  # 27 fps

        frame = cv2.putText(frame, 'FPS: ' + str(fps),
                            (10, frame.shape[0] - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
        frame_num += 1

        _, buffer = cv2.imencode('.jpg', frame)

        frame = buffer.tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
               )  # concat frame one by one and show result
Ejemplo n.º 17
0
 def get_frame(self):
     ret,frame=self.cap.read()
     labels=[]
     gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
     faces=face_classifier.detectMultiScale(gray,1.3,5)
 
     for (x,y,w,h) in faces:
         cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
         roi_gray=gray[y:y+h,x:x+w]
         roi_gray=cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
         
         if np.sum([roi_gray])!=0:
             roi=roi_gray.astype('float')/255.0
             roi=img_to_array(roi)
             roi=np.expand_dims(roi,axis=0)
             global sess
             global graph
             with graph.as_default():
                 set_session(sess)
                 preds=classifier.predict(roi)[0]
                 print("Hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")
                 label=class_labels[preds.argmax()]
                 label_position=(x,y)
                 print(label)
             cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
         else:
             cv2.putText(frame,'No Face Found',(20,20),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),3)
     ret, jpeg = cv2.imencode('.jpg', frame)
     return (jpeg.tobytes())
Ejemplo n.º 18
0
 def get_frame(self):
     success, image = self.video.read()
     # We are using Motion JPEG, but OpenCV defaults to capture raw images,
     # so we must encode it into JPEG in order to correctly display the
     # video stream.
     ret, jpeg = cv.imencode('.jpg', image)
     return jpeg.tobytes()
Ejemplo n.º 19
0
    def run(self):
        # out_name = '{0}_{1}.json'.format(self.camera_id, str(time.time()))
        # out_file_path = os.path.join(self.base_dir, out_name)

        frames_to_write = []
        for _ in range(self.size):
            frame_obj = self.queue.popleft()
            t = frame_obj['time']
            frame_raw = frame_obj['frame']
            _1, buffer = cv2.imencode('.jpg', frame_raw)
            bytes64_encoded = base64.b64encode(buffer)
            bytes64_string = bytes64_encoded.decode('utf-8')
            frames_to_write.append({'time': t, 'frame': bytes64_string})

        # output_file = {
        #    'camera': self.camera_id,
        #    'frames': frames_to_write
        # }

        output_message = {'camera': self.camera_id, 'frames': frames_to_write}

        # f = open(out_file_path, "wb")
        # f.write(json.dumps(output_file).encode('utf-8'))
        # f.close()
        self.kafka_producer.send(
            self.kafka_topic, value=json.dumps(output_message).encode('utf-8'))
Ejemplo n.º 20
0
def _read_img_worker(path, key, compress_level):
    """Read image worker.

    Args:
        path (str): Image path.
        key (str): Image key.
        compress_level (int): Compress level when encoding images.

    Returns:
        str: Image key.
        byte: Image byte.
        tuple[int]: Image shape.

    不要把该函数放到主函数里,否则无法并行。
    """
    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
    if img.ndim == 2:
        h, w = img.shape
        c = 1
    else:
        h, w, c = img.shape
    _, img_byte = cv2.imencode(
        '.png', img, [cv2.IMWRITE_PNG_COMPRESSION, compress_level]
        )
    return (key, img_byte, (h, w, c))
Ejemplo n.º 21
0
    def get_frame(self):
        ret, frame = self.video.read()

        # DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV

        ret, jpeg = cv2.imencode('.jpg', frame)

        return jpeg.tobytes()
def upscale_frame():

    frame_num = 1
    start = time.time()

    # loop over frames from the video stream
    while True:
        _, frame = camera.read()  # read the camera frame

        # start_pred = time.time()
        with tf.device('/device:GPU:1'):
            frame = cv2.resize(frame, (resize_width, resize_height),
                               interpolation=cv2.INTER_AREA)

            # opencv image to tensorflow image
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            frame = tf.image.convert_image_dtype(frame, tf.float32)

            frame = tf.expand_dims(frame, axis=0)

            frame = model.predict(frame)

            frame = tf.cast(255 * (frame + 1.0) / 2.0, tf.uint8)

            # frame = tf.image.convert_image_dtype(frame, tf.float32)

            # frame = model.predict(frame)

            # frame = tf.cast(255 * (frame + 1.0) / 2.0, tf.uint8)

            # tensor to opencv image
            frame = cv2.cvtColor(frame[0].numpy(), cv2.COLOR_RGB2BGR)

            # FPS
            end = time.time()
            duration = end - start
            fps = round(frame_num / duration, 2)
            # 7 fps, 2 times upscale: 320x180 -> 640x360

            frame = cv2.putText(frame, 'FPS: ' + str(fps),
                                (10, frame.shape[0] - 10),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            # # predict_time
            # # 0.08 second/frame, 2 times upscale: 320x180 -> 640x360
            # duration_pred = round(end - start_pred, 3)
            # frame = cv2.putText(frame, 'SPF: ' + str(duration_pred), (10, 30),
            #                                 cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3)

            frame_num += 1

            _, buffer = cv2.imencode('.jpg', frame)

            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
                   )  # concat frame one by one and show result
Ejemplo n.º 23
0
    def get_frame(self):
        # lê e converte o frame de BGR para RGB e redimensiona o width para acelerar o processamento
        ret, frame = self.video.read()
        frame = imutils.resize(frame, width=500)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # executa a detecção de faces
        rects = faceClass.detectMultiScale(gray,
                                           scaleFactor=1.1,
                                           minNeighbors=5,
                                           minSize=(30, 30))

        # ajusta as medidas do rosto para TOP,RIGHT,BOTTOM,LEFT
        rostos = [(y, x + w, y + h, x) for (x, y, w, h) in rects]

        # classifica
        encodings = face_recognition.face_encodings(rgb, rostos)
        names = []

        for encoding in encodings:
            # faz a tentativa de match entre o input e os encodings existentes
            matches = face_recognition.compare_faces(data["encodings"],
                                                     encoding)
            name = "Unknown"

            if True in matches:
                # cria um dicionário com os pontos que combinaram na comparação
                matchedIds = [i for (i, b) in enumerate(matches) if b]
                counts = {}
                # loop over the matched indexes and maintain a count for
                # each recognized face face
                print('[INFO] matches in frame:' + str(len(matchedIds)))
                if (len(matchedIds) > 50):
                    for i in matchedIds:
                        name = data["nomes"][i]
                        counts[name] = counts.get(name, 0) + 1
                        # determine the recognized face with the largest number
                        # of votes (note: in the event of an unlikely tie Python
                        # will select first entry in the dictionary)
                        name = max(counts, key=counts.get)

                        # atualiza lista de nomes
                        names.append(name)
                        # exibe a caixa com nome reconhecido em volta do rosto
                    faces.showFaces(frame, rostos, name, names)
                else:
                    # exibe a caixa com nome UNKNOWN em volta do rosto
                    print('[INFO] matches in frame:' + str(len(matchedIds)))
                    faces.showFaces(frame, rostos, name, None)
            else:
                # exibe a caixa com nome UNKNOWN em volta do rosto
                print('[INFO] no matches found')
                faces.showFaces(frame, rostos, name, None)

        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()
Ejemplo n.º 24
0
def get_frame():
    camera = cv2.VideoCapture(0)
    while True:
        _, img = camera.read()
        imgencode = cv2.imencode('.jpg', img)[1]
        stringData = imgencode.tostring()
        yield (b'--frame\r\n'
               b'Content-Type: text/plain\r\n\r\n' + stringData + b'\r\n')
    del (camera)
Ejemplo n.º 25
0
def to_base64(img):
    # most of this came from https://jdhao.github.io/2020/03/17/base64_opencv_pil_image_conversion/
    img = cv2.imread(img)
    _, im_arr = cv2.imencode('.png', img)
    im_bytes = im_arr.tobytes()
    b64 = base64.b64encode(im_bytes)
    b64 = str(b64).split('\'')[1]  # gets rid of the b'...' stuff
    b64 = "".join(b64.split('\\n'))  # gets rid of the line breaks
    return b64
Ejemplo n.º 26
0
def compress_and_encode(img: np.ndarray, quality=95) -> str:
    encode_param = [cv2.IMWRITE_JPEG_QUALITY, quality]
    success, img_compressed = cv2.imencode('.jpg', img, encode_param)
    # TODO: handle fail of encoding appropriately
    if not success:
        raise ValueError

    img_b64 = base64.b64encode(img_compressed)
    return img_b64.decode('utf-8')
Ejemplo n.º 27
0
def send_frame_to_kafka(frame_obj):
    t = frame_obj['time']
    frame_raw = frame_obj['frame']
    _1, buffer = cv2.imencode('.jpg', frame_raw)
    bytes64_encoded = base64.b64encode(buffer)
    bytes64_string = bytes64_encoded.decode('utf-8')
    output_message = {'camera': CAMERA_ID, 'frame': bytes64_string, 'time': t}
    kafka_producer.send(KAFKA_TOPIC,
                        value=json.dumps(output_message).encode('utf-8'))
    async def loop():
        while True:
            ret, frame = cap.read()  # get frame from webcam
            res, frame = cv2.imencode(".jpg",
                                      frame)  # from image to binary buffer
            data = base64.b64encode(frame)
            await sio.emit("image", data.decode('ascii'))

            await asyncio.sleep(1 / fps)
Ejemplo n.º 29
0
    def run(self, np_image, max_boxes_to_draw=50, min_score_thresh=0.8):
        """Explicitly running the model on a given image.
        Parameters:
        ----------
        np_image: [height, width, depth] np.ndarray
            Numpy array containing RGB image.
        max_boxes_to_draw: int
            Determins how many segmentations can possibly be detected (default is 50).
        min_score_thresh: int,
            Determins on the minimal threshold of confidence (default is 0.8).
        Returns:
        ----------
        [mask, np_image] list, each element has np.ndarray type and original image size.
        """
        height, width, _ = np_image.shape

        # As far as the model has it's own built in .jpg encoder, convert the array into .jpg
        _, encoded_image = cv2.imencode(
            '.jpg', cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR))
        encoded_image = encoded_image.tobytes()

        num_detections, detection_boxes, detection_classes, detection_scores, detection_masks, image_info = self.__process_image__(
            np.array([encoded_image]))

        num_detections = np.squeeze(num_detections.astype(np.int32),
                                    axis=(0, ))
        detection_boxes = np.squeeze(detection_boxes * image_info[0, 2],
                                     axis=(0, ))[0:num_detections]
        detection_scores = np.squeeze(detection_scores,
                                      axis=(0, ))[0:num_detections]
        detection_classes = np.squeeze(detection_classes.astype(np.int32),
                                       axis=(0, ))[0:num_detections]
        instance_masks = np.squeeze(detection_masks,
                                    axis=(0, ))[0:num_detections]
        ymin, xmin, ymax, xmax = np.split(detection_boxes, 4, axis=-1)
        processed_boxes = np.concatenate(
            [xmin, ymin, xmax - xmin, ymax - ymin], axis=-1)
        segmentations = mrcnn_utils.generate_segmentation_from_masks(
            instance_masks, processed_boxes, height, width)

        idx = []
        for i in range(min(num_detections, max_boxes_to_draw)):
            if detection_classes[
                    i] == 44 and detection_scores[i] >= min_score_thresh:
                idx.append(i)

        mask = np.zeros((height, width, 3))

        for i in range(len(idx)):
            mask += segmentations[i, :, :, np.newaxis]

        mask = mrcnn_utils.smooth_contours_on_mask(mask)
        if not mrcnn_utils.check_background_quality(mask, np_image):
            np_image = mrcnn_utils.add_white_background(mask, np_image)

        return [mask, np_image]
Ejemplo n.º 30
0
def gen_frames():
    while True:
        success, frame = camera.read()
        if not success:
            break
        else:
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')