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');
  • What is the last `print` statement that actually executes? – jslatane Oct 20 '21 at 02:55
  • it prints till while loop, and then blank – Harish_Kanoje Oct 20 '21 at 02:58
  • sorry, this is exact lines it prints on PHP page : "till befor while loop worked worked inside while loop worked inside else" – Harish_Kanoje Oct 20 '21 at 02: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. – jslatane Oct 20 '21 at 03:14
  • yes, but when I run the same script from the terminal then it goes through if statement, and gives the output correctly – Harish_Kanoje Oct 20 '21 at 03:21
  • It sounds then like it might be an issue with the way your PHP code is calling the script - [this question](https://stackoverflow.com/questions/18721066/execute-python-in-a-php-script-using-shell-exec) has some suggestions for using `shell_exec` in this way. – jslatane Oct 20 '21 at 03:24
  • Make sure your PHP (e.g. when executed as the www-data user) has permission to run the python script thru the shell_exec command – Ken Lee Oct 20 '21 at 03:25
  • How can I change permissions of cv2 module – Harish_Kanoje Oct 20 '21 at 03:58
  • I found what exactly problem is, cam = cv2.VideoCapture("input/video.mp4") – Harish_Kanoje Oct 21 '21 at 10:50
  • I about line the video values is not getting assigned to cam, cv2 is not able to access this file when called with php. But when I run it from terminal cv2 is able to access the video – Harish_Kanoje Oct 21 '21 at 10:52

0 Answers0