how to execute python in codeigniter after view is completely rendered

Viewed 74

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

  1. browser tab show loading animetion
  2. camera on for 10 seconds then off (noticed from little led light beside webcam)
  3. 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

  1. Move python section to the end of index() function ---> camera is not on
  2. 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")
        });
  1. 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.

1 Answers

you are trying to open the webcam of the server, not the user device. What you can do is trying to open webcam using javascript on the client side (browser), record, and then send it to the server after that run your python to read the recorded video. take a look this link for recording video via webcam https://developers.google.com/web/fundamentals/media/recording-video

Related