0

Not able to read a video file using openCV in Python3.

Code:

import numpy as np
import cv2

cap = cv2.VideoCapture('C:\Users\u266772\Desktop\Video\video2.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Error:

File "", line 4 cap = cv2.VideoCapture('C:\Users\u266772\Desktop\Video\video2.mp4') ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
M Kumar
  • 11
  • 6
  • Possible duplicate of [Why do I get a SyntaxError for a Unicode escape in my file path?](http://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – Ahsanul Haque Apr 23 '17 at 05:03
  • Thanks. The below changes in the code worked: cap = cv2.VideoCapture("C:/Users/u266772/Desktop/Video/video2.mp4") – M Kumar Apr 23 '17 at 05:29

1 Answers1

-1

Just change the way you write file path

import numpy as np
import cv2

cap = cv2.VideoCapture('C:\\\Users\\\u266772\\\Desktop\\\Video\\\video2.mp4')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
tomerpacific
  • 4,704
  • 13
  • 34
  • 52