yinguobing / butterfly

A lightweight python module to load TensorFlow frozen model (a single pb file).
MIT License
7 stars 2 forks source link

cv.resize #3

Closed MacBook-Pro-gala closed 3 years ago

MacBook-Pro-gala commented 3 years ago

I need to cv.resize my cam0 to (1,128,128,32),How should I make changes to the code

yinguobing commented 3 years ago

据我所知OpenCV不支持通道数的resize。如果你需要把三通道的图像扩增到32通道,需要自己制定扩增规则。推荐使用numpy来实现。

MacBook-Pro-gala commented 3 years ago

我搞错了,我的意思是把摄像头的分辨率改为128*128不改变通道数。

 if video_source is not None:

        cap = cv2.VideoCapture(video_source)
        cap.set(3, 128)
        cap.set(4, 128)  # 设置摄像头分辨率,3为高,4为宽

        while True:
            _, frame = cap.read()

            result = fly.run([frame])
            print(result)

            cv2.imshow("Preview", frame)
            if cv2.waitKey(27) == 27:
                break

但是这样并没有效果。

MacBook-Pro-gala commented 3 years ago

我搞错了,我的意思是把摄像头的分辨率改为128*128不改变通道数。

 if video_source is not None:

        cap = cv2.VideoCapture(video_source)
        cap.set(3, 128)
        cap.set(4, 128)  # 设置摄像头分辨率,3为高,4为宽

        while True:
            _, frame = cap.read()

            result = fly.run([frame])
            print(result)

            cv2.imshow("Preview", frame)
            if cv2.waitKey(27) == 27:
                break

但是这样并没有效果。

ValueError: Cannot feed value of shape (1, 720, 1280, 3) for Tensor 'butterfly/Placeholder:0', which has shape '(1, 128, 128, 3)'

yinguobing commented 3 years ago

原来是这样。

代码里的cap对象是VideoCapture的一个实例,所支持的图像分辨率组合取决于具体的硬件与驱动程序。如果你设定的分辨率不在其支持范围内,则是无效的。常见的分辨率比例为 4:3, 16:9。1:1 (128*128)是比较罕见的。

这种情况下你需要使用 resize 函数将读取到的图像缩放为你想要的分辨率。