If I've the below python file:
print("Helloe from Python")
I can execute it from Rust without external crate as below:
use std::process::Command;
let mut cmd = Command::new("python");
cmd.arg("myfile.py");
match cmd.output(){
Err(e) => { println!("There was an error {}", e); },
Ok(o) => unsafe {
println!("Output: {}", String::from_utf8_unchecked(o.stdout))
},
}
And if I want to call it from JavaScript using web_view crate, I can it as:
use std::process::Command;
use web_view::*;
fn main() {
println!("Hello, world!");
let mut cmd = Command::new("python");
cmd.arg("myfile.py");
web_view::builder()
.title("Call Python file")
.content(Content::Html(HTML))
.size(800, 600)
.user_data(())
.invoke_handler(|_, arg| {
match arg {
"python" => {
cmd.arg("opencv.py");
match cmd.output(){
Err(e) => { println!("There was an error {}", e); },
Ok(o) => unsafe {
println!("Output: {}", String::from_utf8_unchecked(o.stdout))
},
}
},
_ => (),
}
Ok(())
}).run()
.unwrap();
}
const HTML: &str = r#"
<!doctype html>
<html>
<body>
<button onclick="external.invoke('python')">Run</button>
</body>
</html>
"#;
I tried to do same, by calling the below python file that is using OpenCV, but could not get it done, what I'm trying to do is displaying the frame
(the one that is displayed by the command cv2.imshow('Frame', frame)
) in the browser/webview
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Any help?
UPDATE
I was able to execute it using [inline-python](https://github.com/fusion-engineering/inline-python)
as below, video output is coming out, but is there a way to display it in the webview?:
#![feature(proc_macro_hygiene)]
use inline_python::python;
use web_view::*;
fn main() {
println!("Hello, world!");
web_view::builder()
.title("Call open CV")
.content(Content::Html(HTML))
.size(800, 600)
.user_data(())
.invoke_handler(|_, arg| {
match arg {
"python" => {
python! {
print("Hi from Python in line")
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imshow("Frame", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
}
},
_ => (),
}
Ok(())
}).run()
.unwrap();
}
const HTML: &str = r#"
<!doctype html>
<html>
<body>
<button onclick="external.invoke('python')">Run</button>
</body>
</html>
"#;
My Cargo.toml
is:
[dependencies]
web-view = "0.6.2"
inline-python = "0.4.1"