4-1Resize_default.py

000: import cv2
001:
002: def __main():
003: cap = cv2.VideoCapture(0, cv2.CAP_V4L)
004: cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
005: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
006:
007: if not cap.isOpened(): # ビデオキャプチャー可能か判断
008: print(“Not Opened Video Camera”)
009: exit()
010:
011: while True:
012: ret, img = cap.read()
013: if ret == False: # キャプチャー画像取得に失敗したら終了
014: print(“Video Capture Err”)
015: break
016:
017: # ここで処理を実行する
018: img = getResize(img)
019:
020: cv2.imshow(“Final result”, img) # 画面表示
021: if cv2.waitKey(10) > -1:
022: break
023:
024: cap.release()
025: cv2.destroyAllWindows()
026:
027: def getResize(src):
028: “””CPUを使用”””
029: basePixSize = 720 # 縦横で大きい辺の変更したいサイズ
030: height = src.shape[0]
031: width = src.shape[1]
032:
033: largeSize = max(height, width) # 大きい方の辺のサイズ
034: resizeRate = basePixSize / largeSize # 変更比率を計算
035:
036: dst = cv2.resize(src, (int(width * resizeRate), int(height * resizeRate)), interpolation=None)
037:
038: return dst
039:
040: if __name__ == ‘__main__’:
041: print(cv2.__version__)
042:
043: __main()