Ask Your Question
0

NameError: name 'cap' is not defined

asked 2019-03-15 04:03:11 -0500

jeevitha gravatar image

updated 2019-03-15 04:55:13 -0500

.

import cv2
import os
dir_path = '/home/ji/YouTubeClips'

for name in os.listdir(dir_path):

#count=count+1
video_id = name.split('.')[0]
cap = cv2.VideoCapture(0)
success,image = vidcap.read()
count = 0
success = True

total_frames = vidcap.get(1)
single_frame = '/home/jeevitha/2017611005/frame_single/image/'+video_id+'.jpg'
if not (os.path.isfile(single_frame) ): 

    video_file_path=os.path.join(dir_path, name)
    print(video_file_path)
    if (cap.isOpened()== False): 
          print("Error opening video stream or file")
    total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))


    if success:
        cv2.imwrite(video_id+'.jpg',image) 
        vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*65000))    # added this line 
    # save frame as JPEG file

to solve this issue

edit retag flag offensive close merge delete

Comments

problem: you copy / pasted snippets without any understanding of it

berak gravatar imageberak ( 2019-03-15 04:15:31 -0500 )edit
berak gravatar imageberak ( 2019-03-15 04:33:32 -0500 )edit

vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*65000)) # added this line

try to find out, why this is wrong (or -- too late)

berak gravatar imageberak ( 2019-03-15 04:34:09 -0500 )edit

you need to open a cv.VideoCapture instance per file you want to read.

(and no, i won't write your program. you've done enough harm with copy / paste already. now it's time to understand, what you're doing ;)

berak gravatar imageberak ( 2019-03-15 04:40:31 -0500 )edit

65000 is a seconds value of frame in video

jeevitha gravatar imagejeevitha ( 2019-03-15 04:41:09 -0500 )edit

yea, but there's no vidcap, no cap and no success defined ever --you never even tried to understand, what you're doing

berak gravatar imageberak ( 2019-03-15 04:44:10 -0500 )edit

no use opening a webcam, when you want to use video files

please stop that blind copy / paste approach -- you'll get nowwhere like this

berak gravatar imageberak ( 2019-03-15 05:00:17 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2019-03-15 08:34:26 -0500

supra56 gravatar image

updated 2019-03-15 08:40:42 -0500

Change this:

cap = cv2.VideoCapture(0)

to:

vidcap = cv2.VideoCapture(0)

Also this too:

if (cap.isOpened()== False):

to:

if (vidcap.isOpened()== False):

And this too:

total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

to:

total = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))

And for heaven sake in line 5 set indentation probably. Change this:

cv2.imwrite(video_id+'.jpg',image)

to:

cv2.imwrite('video_id.jpg',image)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-15 04:03:11 -0500

Seen: 507 times

Last updated: Mar 15