I'm making a medical web application (using codeigniter 3) that able to record a video from webcam when the user entered "Diagnosis" page .
However, when I tried to execute a python file via these codes this is what happened when I entered Diagnosis page from previous page
- browser tab show loading animetion
- camera on for 10 seconds then off (noticed from little led light beside webcam)
- display Diagnosis page
Controller/Diagnosis
class Diagnosis extends CI_controller
{
public function __construct()
{
parent::__construct();
$this->load->model('diagnosis_model');
}
public function index()
{
$face = $this->diagnosis_model->get_face('female');
## execute python file
$command = escapeshellcmd('vdocapture.py');
shell_exec($command);
## render page
$this->load->view(
'diagnosis/_diagnosis',
array(
'face'=> $face
)
);
}
}
python
import cv2
import time
id = '[citizen_id]'
cap = cv2.VideoCapture(0)
## set timeout
timeout = time.time() + 10 # 10 sec
## Define the codec and create VideoWriter object
## Save video in "capture" folder
out = cv2.VideoWriter('capture/%s.wmv' % id, -1, 29, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
## write the frame
out.write(frame)
if time.time() > timeout:
break
else:
break
## Release everything if job is finished
cap.release()
out.release()
But what I expected is letting view render until completed then execute python and start recording video in background for 10 seconds (while the user can interact with form normally).
What I've tried are
- Move python section to the end of index() function ---> camera is not on
- Using ajax technique from how to execute a codeigniter controller method in background and action after render a view (codeigniter) ---> not sure what I've misses but camera still not on
Controller/Diagnosis
public function run_py(){
$command = escapeshellcmd('vdocapture.py');
shell_exec($command);
}
javascript (first try)
$.ajax("index.php?/Diagnosis/run_py()")
javascript (second try)
$.ajax({
url: "Diagnosis/run_py",
async: true
}).done(function() {
console.log("Capturing began")
});
- I tried to change directory of vdocapture.py and capture folder without changing anything from my original code except vdocapture.py and capture folder directory --> camera still not on after view has rendered
from
|--MY_CI_Project
|--Application
|--index.php
|-- **capture**
|-- **vdocapture.py**
to
|--MY_CI_Project
|--Application
|--controller
|--model
|--view
|-- **capture**
|-- **vdocapture.py**
|--index.php
So, how can I execute a python script after CI view is rendered?
Best regards.