-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_capture.zig
More file actions
60 lines (48 loc) · 2.09 KB
/
camera_capture.zig
File metadata and controls
60 lines (48 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const std = @import("std");
const cv = @import("zopencv");
pub fn main() !void {
std.debug.print("=== Live Camera Capture ===\n", .{});
std.debug.print("Captures from camera and applies real-time edge detection\n", .{});
std.debug.print("Press ESC to quit\n\n", .{});
var cap = try cv.videoio.VideoCapture.init();
defer cap.deinit();
const opened = cap.openDevice(0);
if (!opened or !cap.isOpened()) {
std.debug.print("⚠️ No camera found. This example requires a connected camera.\n", .{});
std.debug.print(" Tip: Try the video_capture example for file-based processing.\n", .{});
return;
}
const width = cap.get(.frame_width);
const height = cap.get(.frame_height);
std.debug.print("✅ Camera opened: {d:.0}x{d:.0}\n\n", .{ width, height });
var frame = try cv.Mat.init();
defer frame.deinit();
var gray = try cv.Mat.init();
defer gray.deinit();
var edges = try cv.Mat.init();
defer edges.deinit();
// Create windows
try cv.highgui.namedWindow("Camera", .autosize);
defer cv.highgui.destroyWindow("Camera") catch {};
try cv.highgui.namedWindow("Edges", .autosize);
defer cv.highgui.destroyWindow("Edges") catch {};
var frame_count: u32 = 0;
while (cap.read(&frame)) {
if (frame.empty()) break;
frame_count += 1;
cv.imgproc.cvtColor(frame, &gray, .bgr2gray);
cv.imgproc.gaussianBlur(gray, &gray, cv.Size{ .width = 5, .height = 5 }, 1.4, 0, 0);
cv.imgproc.canny(gray, &edges, 50, 150, 3, false);
try cv.highgui.imshow("Camera", frame);
try cv.highgui.imshow("Edges", edges);
if (frame_count == 1) {
try cv.imgcodecs.imwrite("examples/tmp/camera_frame.jpg", frame);
try cv.imgcodecs.imwrite("examples/tmp/camera_edges.jpg", edges);
std.debug.print("💾 Saved first frame\n", .{});
}
const key = cv.highgui.waitKey(1);
if (key == 27) break;
}
std.debug.print("\n📊 Captured {d} frames\n", .{frame_count});
std.debug.print("🎉 Camera capture complete!\n", .{});
}