-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Image::from_opencv_mat() and opencv as feature
- Loading branch information
Michal Conos
committed
Sep 14, 2024
1 parent
33febba
commit bdede6e
Showing
7 changed files
with
237 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::{env::args, time::Instant}; | ||
|
||
use opencv::{ | ||
core::{Mat, MatTrait, MatTraitConst, Rect, Scalar, Vector}, | ||
highgui::{destroy_all_windows, imshow, wait_key}, | ||
imgcodecs::imwrite, | ||
imgproc::{rectangle, LINE_8}, | ||
videoio::{VideoCaptureTrait, CAP_ANY}, | ||
}; | ||
use tch::{Kind, Tensor}; | ||
use yolo_v8::image::Image; | ||
|
||
fn main() -> Result<(), opencv::Error> { | ||
let filename = args().nth(1).unwrap_or("test3.mp4".to_owned()); | ||
println!("filename={filename}"); | ||
let mut cap = opencv::videoio::VideoCapture::from_file(&filename, CAP_ANY)?; | ||
let yolo = yolo_v8::YoloV8ObjectDetection::with_model(yolo_v8::YOLOModel::Nano); //.post_process_on_cpu(); | ||
let device = tch::Device::cuda_if_available(); | ||
println!("device: {:?}", device); | ||
|
||
loop { | ||
let mut timings = Vec::new(); | ||
let mut frame = Mat::default(); | ||
let start = Instant::now(); | ||
let have_image = cap.read(&mut frame)?; | ||
if !have_image { | ||
break; | ||
} | ||
|
||
let mut image = Image::from_opencv_mat(&frame, (640, 640))?; | ||
timings.push(("read_frame", start.elapsed())); | ||
let start = Instant::now(); | ||
let predictions = yolo.predict(&image, 0.25, 0.7); //.postprocess(); | ||
timings.push(("detection", start.elapsed())); | ||
// image.draw_rectangle(&predictions); | ||
// image.save("result.jpg"); | ||
|
||
let start = Instant::now(); | ||
let predictions = predictions.postprocess(); | ||
timings.push(("postprocess", start.elapsed())); | ||
let start = Instant::now(); | ||
for bbox in predictions.0 { | ||
let w = bbox.xmax - bbox.xmin; | ||
let h = bbox.ymax - bbox.ymin; | ||
let class = format!("{} {}%", bbox.name, (bbox.conf * 100.0) as i32); | ||
let _ = opencv::imgproc::put_text( | ||
&mut frame, | ||
&class, | ||
(bbox.xmin as i32, bbox.ymin as i32).into(), | ||
0, | ||
1.0, | ||
Scalar::new(255.0, 255.0, 255.0, 255.0), | ||
1, | ||
LINE_8, | ||
false, | ||
); | ||
rectangle( | ||
&mut frame, | ||
Rect::new(bbox.xmin as i32, bbox.ymin as i32, w as i32, h as i32), | ||
Scalar::new(255.0, 128.0, 0.0, 255.0), | ||
2, | ||
1, | ||
0, | ||
)?; | ||
} | ||
|
||
imshow("Image", &frame)?; | ||
let key = wait_key(1)?; | ||
if key > 0 && key != 255 { | ||
break; | ||
} | ||
timings.push(("draw_boxes", start.elapsed())); | ||
println!("timings:{:?}", timings); | ||
} | ||
cap.release()?; | ||
destroy_all_windows()?; | ||
Ok(()) | ||
} | ||
|
||
fn square64(size: i64, w: i64, h: i64) -> (i64, i64) { | ||
let aspect = w as f32 / h as f32; | ||
if w > h { | ||
let tw = size; | ||
let th = (tw as f32 / aspect) as i64; | ||
(tw, th) | ||
} else { | ||
let th = size; | ||
let tw = (size as f32 * aspect) as i64; | ||
(tw, th) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters