I’m trying to create a project in Windows that integrates a pre-trained Faster R-CNN model with ResNet-50 and Feature Pyramid Network (FPN) for object detection, using Rust and Python. The program processes images in a specified folder, detects objects in each image, and displays the results in a simple Python GUI application.
My problem is that I can’t get OpenCV to work. What do is:
- I’m creating new project with
cargo new object_detection
- Then virtual environment with
python -m venv venv
- And then use
develop maturin
to deploy the package from Rust so that it can be referenced from Python
lib.rs:
use pyo3::prelude::*;
use opencv::prelude::*;
use opencv::imgcodecs;
use tch::vision::detector::{ObjectDetector, VisionTask};
use std::fs;
use rayon::prelude::*;
#[pyfunction]
fn process_images(folder_path: &str) -> PyResult<String> {
// Load the pre-trained model
let detector = ObjectDetector::new("./model/fasterrcnn_resnet50_fpn.pth")
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
let paths: Vec<_> = fs::read_dir(folder_path)
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?
.filter_map(|entry| entry.ok())
.collect();
let results: Vec<String> = paths.par_iter().map(|path| {
let path = path.path();
if path.is_file() {
match imgcodecs::imread(path.to_str().unwrap(), imgcodecs::IMREAD_COLOR) {
Ok(img) => {
// Convert OpenCV Mat to Tensor
let tensor = tch::vision::image::load(path.to_str().unwrap()).unwrap();
match detector.detect(&tensor) {
Ok(detections) => format!("Results for {}: {:?}n", path.display(), detections),
Err(e) => format!("Error detecting objects in {}: {}n", path.display(), e),
}
},
Err(e) => format!("Error reading image {}: {}n", path.display(), e),
}
} else {
format!("{} is not a filen", path.display())
}
}).collect();
Ok(results.concat())
}
#[pymodule]
fn object_detection(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(process_images, m)?)?;
Ok(())
}
GUI.py:
import tkinter as tk
from tkinter import filedialog
import object_detection
def upload_folder():
folder_path = filedialog.askdirectory()
if folder_path:
process_images(folder_path)
def process_images(folder_path):
results = object_detection.process_images(folder_path)
display_results(results)
def display_results(results):
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, results)
root = tk.Tk()
root.title("Object Detection Application")
upload_button = tk.Button(root, text="Upload Folder", command=upload_folder)
upload_button.pack()
result_text = tk.Text(root)
result_text.pack()
root.mainloop()
And when I try to create package with maturin develop
I get an error: note: LINK : fatal error LNK1181: cannot open input file 'libclang.lib'
.
Before that I installed sufficient packages with choco for Windows:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
And then:
choco install llvm opencv
I explicitly created environment variables:
LIBCLANG_PATH = C:Program FilesLLVMlib
OPENCV_INCLUDE_PATHS = C:toolsopencvbuildinclude
OPENCV_LINK_LIBS = opencv_world490
OPENCV_LINK_PATHS = C:toolsopencvbuildx64vc16lib
My Cargo.toml looks like this:
[package]
name = "object_detection"
version = "0.1.0"
edition = "2021"
[dependencies]
pyo3 = { version = "0.15", features = ["extension-module"] }
opencv = "0.92.0"
tch = "0.1"
rayon = "1.5"
[lib]
crate-type = ["cdylib"]
It doesn’t work with the same error after building this package with cardo build
.
What might be the issue?