11-3TimeShift.py

000: import cv2
001: import numpy as np
002:
003: def __main():
004: width = 1280.
005: height = 720.
006: cap = cv2.VideoCapture(0, cv2.CAP_V4L)
007: cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
008: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
009:
010: if not cap.isOpened(): # ビデオキャプチャー可能か判断
011: print(“Not Opened Video Camera”)
012: exit()
013:
014: while True:
015: ret, img = cap.read()
016: img = cv2.flip(src=img, flipCode=1)
017: buf = videoBuf(img)
018: cv2.imshow(“Late”, buf)
019: cv2.imshow(“Real”, img)
020: if cv2.waitKey(10) > -1:
021: break
022:
023: cap.release()
024: cv2.destroyAllWindows()
025: return 0
026:
027: def videoBuf(img):
028: global frame
029: global buffer
030:
031: buffer.append(img)
032: frame += 1
033: print(frame)
034: if frame > 200:
035: dst = buffer.pop(0)
036: else:
037: size = img.shape
038: dst = np.zeros(size, dtype=np.uint8)
039:
040: return dst
041:
042: if __name__ == ‘__main__’:
043: print(cv2.__version__)
044: buffer = list()
045: frame = 0
046: __main()