Opencv - capturing frames from video and then storing it 90 degree counter clockwise

Hello!! i am trying to make dataset by capturing frames from video using opencv(cv2) but it is storing like 50 percent of image which is 90 degree rotated despite of i have not coded for rotation anywhere in the code. I want all the images to be stored without being rotated. please refer code below

import cv2
import skvideo.io
import glob
from google.colab.patches import cv2_imshow
import os
dir='/content/gdrive/MyDrive/J'
cap = skvideo.io.vread('/content/gdrive/MyDrive/videos/J/j133.mp4')
files = glob.glob('/content/gdrive/MyDrive/videos/J/*.mp4')
nc=0
for f in files:
	cap = cv2.VideoCapture(f)
	time_skips = float(400) #skip 
	x=0
	temp=f.split('/')[-1][:-4]
	lfn=cap.get(cv2.CAP_PROP_FRAME_COUNT)
	lfn-=1
	cap.set(cv2.CAP_PROP_POS_FRAMES, int(lfn))
	success,img=cap.read()
	
	fl=1
	for i in range(8):
		x+=1
		count = 1
		nc+=1
		cap.set(cv2.CAP_PROP_POS_MSEC, ( x*60))
		success,image = cap.read()
		path=os.path.join(dir,'j'+str(nc))
		try:
			os.mkdir(path)
		except:
			continue
		while count<=3:               
			cap.set(cv2.CAP_PROP_POS_MSEC, (count*time_skips + x*60))
			cv2.imwrite(os.path.join(path, 'j'+str(nc)+str(count)+'.jpg'),image) 
			success,image = cap.read()
			if success==False:
				while success == False:
					lfn-=1
					cap.set(cv2.CAP_PROP_POS_FRAMES, int(lfn))
					success,img=cap.read()
				image=img
			count += 1 
	cap.release()

Please suggest how i can make this code work properly… Any suggestion is highly appreciated.
Thanks in advance

ps: somewhere i read that opencv does not check metadata of video but have no idea about it.

could you please try to clean up your code ?
(remove commented code, unneeded imports, etc),
so ppl here can at least try to reproduce it ? thank you !

1 Like

yes, done!! thankyou !

(the formatting wasn’t to my liking so I took the liberty)

nothing in your code would cause pictures to be rotated.

what are you saying?

  • is your code rotating some pictures or not?
  • do you want some pictures rotated or not?

and how do you determine that pictures are rotated? present EVERYTHING you do. you probably do things we can’t even imagine.

1 Like

sorry!! this is my first post. Hope the edited post was more clear. Any suggestions are highly appreciated. Thanks

when I said “everything” I hoped you’d present everything. oh well.

I’m betting something rotates these pictures but it’s not OpenCV.

use imread to read these pictures, check the .shape of the returned array.

actually, also do that to the frames you read from the video, as you read them.

you are dealing with colab and google drive and possibly accessing this from android or who knows what operating system. all of these could be lying to you.

1 Like