Hi. I am trying to extract and save 3 random frames from the middle third of video files in a directory. I started with this crude code (I know it is poorly written) . I could figure the number of total frames, the range of the middle 3rd of frames, and saving every nth number from that range. First issue is that random.sample gives an error to even begin to get a random image. The second all that works for a single video file when I need multiples (each to its own directory) I appreciate any help. Thank you

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hi. I am trying to extract and save 3 random frames from the middle third of video files in a directory. I started with this crude code (I know it is poorly written) . I could figure the number of total frames, the range of the middle 3rd of frames, and saving every nth number from that range. First issue is that random.sample gives an error to even begin to get a random image. The second all that works for a single video file when I need multiples (each to its own directory) I appreciate any help. Thank you 

1
2
main67
3
4
5
14
15
16
17
18
19
20
21
22
23
24
25
26
8 length
int(cv2.VideoCapture.get(cap, property_id))
9 print('Number of frames is: ,length)
10
11
12
13
27
28
29
30
31
import cv2
import os
import random
32
33
34
35
36
vidcap cv2.VideoCapture('SampleVideo_1280x720_10mb.mp4')
cap cv2.VideoCapture("SampleVideo_1280x720_10mb.mp4")
property_id = int(cv2.CAP_PROP_FRAME_COUNT)
=
X
int(length/3)
print('Frame begining at the second third is ', X, 'th')
Y = 2*X
print('Frame at the end of second third is ', Y, 'th')
print('From the ', X, 'th frame to the ', Y, 'the frame, we exreact every 20th frame' )
=
success, image vidcap.read()
count =
images
[]
path
'C:/Users/user/Desktop/get_3/images'
while success and count < (Y-X):
#cv2.imwrite(os. path. join(path 'waka.jpg'), img)
#cv2.imwrite(os.path.join(path, "frame%d.jpg" % count, image))
start_frame_number)
"frame%d.jpg" % count), image)
#cv2.imwrite("frame%d.jpg" % count, image) #save frame as JPEG file
#success, image
=
vidcap.read()
print('Read a new frame: ', success)
count += 20
#images_random
start_frame_number
success, image
=
vidcap.set(cv2.CAP_PROP_POS_FRAMES,
X
vidcap.read()
=
cv2.imwrite(os.path.join(path,
images.applend (image)
random.sample(images, 3)
Transcribed Image Text:1 2 main67 3 4 5 14 15 16 17 18 19 20 21 22 23 24 25 26 8 length int(cv2.VideoCapture.get(cap, property_id)) 9 print('Number of frames is: ,length) 10 11 12 13 27 28 29 30 31 import cv2 import os import random 32 33 34 35 36 vidcap cv2.VideoCapture('SampleVideo_1280x720_10mb.mp4') cap cv2.VideoCapture("SampleVideo_1280x720_10mb.mp4") property_id = int(cv2.CAP_PROP_FRAME_COUNT) = X int(length/3) print('Frame begining at the second third is ', X, 'th') Y = 2*X print('Frame at the end of second third is ', Y, 'th') print('From the ', X, 'th frame to the ', Y, 'the frame, we exreact every 20th frame' ) = success, image vidcap.read() count = images [] path 'C:/Users/user/Desktop/get_3/images' while success and count < (Y-X): #cv2.imwrite(os. path. join(path 'waka.jpg'), img) #cv2.imwrite(os.path.join(path, "frame%d.jpg" % count, image)) start_frame_number) "frame%d.jpg" % count), image) #cv2.imwrite("frame%d.jpg" % count, image) #save frame as JPEG file #success, image = vidcap.read() print('Read a new frame: ', success) count += 20 #images_random start_frame_number success, image = vidcap.set(cv2.CAP_PROP_POS_FRAMES, X vidcap.read() = cv2.imwrite(os.path.join(path, images.applend (image) random.sample(images, 3)
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY