0

I am facing a problem with the python script when called by PHP.

The python script is working great when I run it from the terminal.

but when I call the python script from PHP it runs half, when statements of the cv2 module come the code does not work

I have imported cv2 module

the python script is

video2frames.py

    # Importing all necessary libraries 
import cv2 
import os 


# Read the video from specified path 
cam = cv2.VideoCapture("input/video.mp4") 

try: 
    
    # creating a folder named data 
    if not os.path.exists('input/frames'): 
        os.makedirs('input/frames') 

# if not created then raise error 
except OSError: 
    print ('Error: Creating directory of frames') 

# frame 
currentframe = 0

print('till before while loop worked')

a = 1
 
while(a<250): 
    # reading from frame 
    print('worked inside while loop')
    ret,frame = cam.read() 
    
    if ret: 
        # if video is still left continue creating images 
        print('worked inside if condition')
        name = './input/frames/frame' + str(currentframe) + '.png'
        print ('Creating...' + name) 
        # writing the extracted images 
        cv2.imwrite(name, frame) 
        # increasing counter so that it will 
        # show how many frames are created 
        currentframe += 1
        print('worked inside if')
    else:
        print('worked inside else');break

# Release all space and windows once done 
cam.release() 
cv2.destroyAllWindows()

the PHP code I used to call the python script is

shell_exec('python3 video2frames.py');
10
  • What is the last print statement that actually executes? Commented Oct 20, 2021 at 2:55
  • it prints till while loop, and then blank Commented Oct 20, 2021 at 2:58
  • sorry, this is exact lines it prints on PHP page : "till befor while loop worked worked inside while loop worked inside else" Commented Oct 20, 2021 at 2:59
  • That's great info. It sounds like ret is not getting set to True so your code to extract the frames is never executing. You could verify this by printing ret. You'll need to look at where this is getting set: cam.read() to determine why that would be - maybe check your file or make sure you are using the function correctly. Commented Oct 20, 2021 at 3:14
  • yes, but when I run the same script from the terminal then it goes through if statement, and gives the output correctly Commented Oct 20, 2021 at 3:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.