当前位置:   article > 正文

opencv读取图片cv2格式转换_cv2文件转换器

cv2文件转换器

1、字节转字符串:

def bytes2str(b):
    return base64.b64encode(b).decode()
  • 1
  • 2
'
运行

2、字符串转字节:

def str2bytes(s):
    return base64.b64decode(s.encode())
  • 1
  • 2
'
运行

3、cv2格式转为字符串:

        image_encode = cv2.imencode('.jpg', np.asarray(img_raw))[1]
        image_str = str(base64.b64encode(image_encode))[2:]
  • 1
  • 2

4、字符串转cv2:

def str2np_cv2(img_str):
    img_bytes = base64.b64decode(img_str)
    img_np = np.frombuffer(img_bytes, dtype=np.uint8)
    img_np_cv2 = cv2.imdecode(img_np, cv2.IMREAD_COLOR)
    return img_np_cv2
  • 1
  • 2
  • 3
  • 4
  • 5
'
运行

5、根据人脸坐标定位,画框:

    if score <0.8:
        text="Not clear"
        color=(0,0,255)
    else:
        text="clear"
        color=(0,255,0)
    cv2.rectangle(img_np_cv2, (int(ymin), int(xmin)), (int(ymax), int(xmax)),color, 2)
    cv2.putText(img_np_cv2, "%s: %.2f" % (text, score), (int(xmin), int(ymin)), cv2.FONT_HERSHEY_SIMPLEX,1,color,2)
    #最后一个表示字体粗细,color表示字体颜色,1表示字体大小。

    cv2.namedWindow('image', 0)
    cv2.resizeWindow("image", 600, 600)
    cv2.imshow('image', img_np_cv2)
    cv2.waitKey(waitkey)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

6、利用本地摄像头获取视频并显示:

def run_on_video(video_path=0, output_video_name, conf_thresh):
	#0为默认摄像头
    cap = cv2.VideoCapture(video_path)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    fps = cap.get(cv2.CAP_PROP_FPS)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    # writer = cv2.VideoWriter(output_video_name, fourcc, int(fps), (int(width), int(height)))
    total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    if not cap.isOpened():
        raise ValueError("Video open failed.")
        return
    status = True
    idx = 0
    
    while status:
        start_stamp = time.time()
        status, img_raw = cap.read()
        img_raw = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
        read_frame_stamp = time.time()
        if (status):
            inference(img_raw,
                      conf_thresh,
                      iou_thresh=0.5,
                      target_shape=(360, 360),
                      draw_result=True,
                      show_result=False)

            cv2.imshow('image', img_raw[:, :, ::-1])
            cv2.waitKey(1)
            inference_stamp = time.time()
            # writer.write(img_raw)
            write_frame_stamp = time.time()
            idx += 1
            print("%d of %d" % (idx, total_frames))
            print("read_frame:%f, infer time:%f, write time:%f" % (read_frame_stamp - start_stamp,
                                                                   inference_stamp - read_frame_stamp,
                                                                   write_frame_stamp - inference_stamp))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

7、waitkey

cv2.waitKey(1) #不需要点击
cv2.waitKey(0)#点击之后,才可以结束



  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/823808
推荐阅读
相关标签
  

闽ICP备14008679号