OpenCV, sometimes captured images rotated upside down

import cv2 as cv
import numpy as np

def view_camera_image():

im_width = 640
im_height = 480

cap1 = cv.VideoCapture(“v4l2src device=/dev/video0 ! image/jpeg, width=”+str(im_width)+“, height=”+str(im_height)+" ! jpegdec ! videoconvert ! appsink")

if cap1.isOpened():
print(“Cap1 is Open”)
cv.namedWindow(“Camera : 1”, cv.WINDOW_AUTOSIZE)
else:
print(“Cap 1 is Closed”)
cap1 = None

if(cap1 != None) :
result1, image1 = cap1.read()
if result1:
cv.imshow(“Camera : 1” , image1)
cv.waitKey(1)
if(cap1 != None) :
cap1.release()

view_camera_image()

Above mentioned program running environment as follows,
Device: Jetson nano J3010
OS: Ubuntu 20.04
Python 3.8.10
OpenCV version: 4.2.0
numpy version: 1.17.4
While running with these conditions we came across an issue, sometimes captured images rotated upside down (180 degrees rotation).
For this issue, we found one solution, it says’s OpenCV latest version solves this rotation issue.

According to that we also upgrade OpenCV to 4.10 latest version using the “pip install opencv-python” command.
But above mentioned code is not working with the latest OpenCV version.
Please guide us to see the solution.