cv2 unable to access video when called with php - python

I have written a code where I assign a video file to the cv2 module in python script.
I am running this python script with PHP. when I run the PHP in the terminal then cv2 can access the video, but when I run the PHP on the webpage then cv2 cannot access the video.
python filename - video2frames.py
PHP filename - script.php
bootscript.php -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>script</title>
</head>
<body>
<?
$out = shell_exec("python3 video2frames.py");
echo "$out";
?>
video2frames.py -
# Importing all necessary libraries
import sys
import os
import cv2
value = 'cv2' in sys.modules
print('Check if true cv2 is imported',value)
# Read the video from specified path
cam = cv2.VideoCapture('video.mp4')
print('cam value : ',cam)
output in terminal -
[customro#heimdall www]$ php bootscript.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>script</title>
</head>
<body>
Check if true cv2 is imported True
cam value : <VideoCapture 0x7f2731224fd0>
</body>
</html>
output in webpage -
Check if true cv2 is imported True cam value :
I Have tried -
changing permissions of all the files associated with this program to 777
I have tried exec, system, shell_exec commands to call python file in PHP

Related

Emulating CORS issues with pytest

I need to test requests that can be sent through iframe. For example: i have some page on domain_01:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
body {
margin: 0 auto;
}
</style>
<body>
<iframe id="inlineFrameExample"
title="Inline Frame Example"
width="1600"
height="900"
src="http://domain_02:8000/app/dashboard">
</iframe>
</body>
</html>
And as you can see here this page contains iframe with link to page on domain_02. I try to understand: is it possible to emulate request that goes to domain_02 through this iframe on doamin_01 with pytest.
Main task what i need to solve it's create tests with different requests and check that there is no CORS issues with it.
How i check it now: manually only. I run second web-server through inner python server (python -m http.server 8090) and set dns-record on local dns-server to emulate domain_01. It will be so cool to run this tests with pytest.

JsException(TypeError: Failed to fetch)

When I try to import a pyscript source code to my HTML it shows a "JsException(TypeError: Failed to fetch)" error.
helloworld.py
print("Hello World")
testPyscript.html
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<title></title>
</head>
<body>
<py-script src="helloworld.py">
("Another Text Test")
</py-script>
</body>
</html>
I was having the same problem and found the answer here: PyScript: Loading Python Code in the Browser
The problem is <py-script src="helloworld.py"> do not support loading local files, you need a server for the browser to load it...
Go into the folder you keep the files and run python -m http.server 80 and then, on the browser, go to localhost/testPyscript.html
Hope it helps
For some reason your directory which contains helloworld.py and testPyscript.html need to run in localhost, open your folder in vsCode and install live server from extensions then in your right bottom corner press on Go Live. you will be directed to the default browser with the expected output from helloworld.py

How do I eliminate extra line in Python multi-line string?

I made a HTML Basic Markup string in Python and I made the string split over multiple lines, however, I ran into a problem. This is an HTML Basic Markup string and I want to to appear like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled</title>
</head>
<body>
</body>
</html>
So I created a string in python and this is what it looks like:
HTML_Basic_Markup = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled</title>
</head>
<body>
</body>
</html>
"""
When I print HTML_Basic_Markup I get an extra space at the top, so to fix this I did this:
HTML_Basic_Markup = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Untitled</title>
</head>
<body>
</body>
</html>
"""
However, I want to make the code look neat and want the Doctype to be aligned with the rest of the code, so how would I remove the line which is created at the top?
String objects support a strip method you can use to remove leading and trailing characters (including newlines). See here.

Python Eel. Python function does not start using from javascript

Sorry for my English.
I am want to study python library (eel). I am wrote the following code as in the example from the documentation:
The "index.html" file is located in the "Web" folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
eel.python_func("Hello from Python");
</script>
</head>
<body>
Hello!!
</body>
</html>
The "main.py" file is located one level above the "index.html" file:
import eel
eel.init("web")
#eel.expose
def python_func(text):
print(text)
eel.start("index.html")
Running main.py displays a browser window that says "Hello !!" but python_func() dont started. Message "Hello from Python" dont displayed in terminal.
I am tried different options for solutions to problem:
reinstalled eel
reinstalled python
pasted the python_func () function code right after import.
But this solutions not helped.
I ask for help in solving my problem.
I am using Python 3.7.6 32 bit

Why can't I console log data from my flask application? I am using jinja

I am trying load data into my flask page, and console log it, however I keep getting the below error
VM165:1 Uncaught ReferenceError: data is not defined
at <anonymous>:1:13
These are the code snippets from my .py and .html
import os
from flask import Flask, render_template, jsonify, request, redirect
import json
app = Flask(__name__)
#app.route("/")
def home():
json_file = open("/Users/-------/Documents/GitHub/Geospatial-Opportunities-for-Supermarkets/supermarket_locations/longos_locations.json", "r", encoding ="utf-8")
data = json.load(json_file)
json_file.close
return render_template("index.html", data = data)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Supermarkets in Toronto</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.0.0-rc.3/dist/leaflet.css" />
<link rel="stylesheet" type="text/css" href="static/css/style.css">
<script> var data = '{{data}}'; </script>
</head>
Looks like I had 2 index.html files and I was using the wrong one

Categories