Skip to content

Commit 0096614

Browse files
committed
Futile compilation attempt working. see new file.
1 parent 9cde73c commit 0096614

8 files changed

Lines changed: 148 additions & 5 deletions

File tree

bridge/include/bridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ proto_bridge_simple(tanhshrink);
107107
void split_loop(int64_t idx, int64_t n);
108108
void split_loop_filler(int64_t n,int64_t* ret);
109109

110+
void show_webcam(void);
111+
110112

111113
// bridge_tensor_t conv2d(
112114
// bridge_tensor_t input,

bridge/include/bridge.h.pch

1.13 MB
Binary file not shown.

bridge/lib/bridge.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <chrono>
1717
#include <thread>
1818

19+
#include <opencv2/opencv.hpp>
20+
21+
1922
#define def_bridge_simple(Name) \
2023
extern "C" bridge_tensor_t Name(bridge_tensor_t input) { \
2124
auto t_input = bridge_to_torch(input); \
@@ -397,4 +400,41 @@ extern "C" void split_loop_filler(int64_t n,int64_t* ret) {
397400
*ret = i;
398401
std::this_thread::sleep_for(std::chrono::seconds(0));
399402
}
403+
}
404+
405+
406+
407+
cv::VideoCapture open_camera(int cam_index) {
408+
cv::VideoCapture cap(cam_index, cv::CAP_AVFOUNDATION);
409+
if (!cap.isOpened()) {
410+
std::cerr << "Could not open camera index " << cam_index << std::endl;
411+
return cv::VideoCapture();
412+
}
413+
cap.set(cv::CAP_PROP_BUFFERSIZE, 1); // minimal internal buffering
414+
cap.set(cv::CAP_PROP_FPS, 60); // request higher FPS if possible
415+
return cap;
416+
}
417+
418+
419+
extern "C" void show_webcam(void) {
420+
cv::VideoCapture cap;
421+
cap = open_camera(0);
422+
423+
cv::Mat frame_bgr;
424+
425+
while (true) {
426+
if (!cap.read(frame_bgr) || frame_bgr.empty()) {
427+
std::cerr << "[WARN] Empty frame, exiting" << std::endl;
428+
break;
429+
}
430+
431+
cv::imshow("webcam", frame_bgr);
432+
433+
if (cv::waitKey(1) == 27) { // ESC key
434+
break;
435+
}
436+
}
437+
438+
cap.release();
439+
cv::destroyAllWindows();
400440
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/usr/bin/clang++ -std=c++20 style_transfer.cpp -o styletransfer -I ../../../libtorch/include -I ../../../libtorch/include/torch/csrc/api/include -I ../include $(pkg-config --cflags --libs opencv4) -L ../../../libtorch/lib -ltorch -ltorch_cpu -lc10 -ltorch_global_deps
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
int main() {
5+
cv::VideoCapture cap(0); // Open the default camera (0)
6+
if (!cap.isOpened()) {
7+
std::cerr << "Error: Could not open camera." << std::endl;
8+
return -1;
9+
}
10+
11+
cv::Mat frame;
12+
while (true) {
13+
cap >> frame; // Capture a new frame
14+
if (frame.empty()) {
15+
std::cerr << "Error: Could not capture frame." << std::endl;
16+
break;
17+
}
18+
19+
cv::imshow("Webcam", frame); // Display the captured frame
20+
if (cv::waitKey(30) >= 0) break; // Exit on any key press
21+
}
22+
23+
cap.release(); // Release the camera
24+
cv::destroyAllWindows(); // Close all OpenCV windows
25+
return 0;
26+
}

examples/split_loop/CMakeLists.txt

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,86 @@
1+
find_package(OpenCV 4 REQUIRED)
2+
3+
find_library(ACCELERATE Accelerate REQUIRED)
4+
find_library(METAL Metal REQUIRED)
5+
find_library(FOUNDATION Foundation REQUIRED)
6+
7+
8+
9+
add_library(bridge_cv OBJECT ${BRIDGE_DIR}/include/bridge.h ${BRIDGE_DIR}/lib/bridge.cpp)
10+
11+
target_link_directories(bridge_cv PRIVATE ${LIBTORCH_DIR}/lib)
12+
13+
target_link_libraries(
14+
bridge_cv
15+
PRIVATE
16+
-ltorch
17+
-ltorch_cpu
18+
-lc10
19+
-ltorch_global_deps
20+
${OpenCV_LIBS}
21+
# ${TORCH_LIBRARIES}
22+
${ACCELERATE}
23+
${METAL}
24+
${FOUNDATION}
25+
)
26+
27+
target_include_directories(
28+
bridge_cv
29+
PRIVATE
30+
${BRIDGE_DIR}/include
31+
${LIBTORCH_DIR}/include
32+
${LIBTORCH_DIR}/include/torch/csrc/api/include
33+
# ${BRIDGE_DIR}/util
34+
)
35+
36+
# if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
37+
# target_compile_options(bridge_cv PRIVATE -Ofast -flto -ffast-math)
38+
# target_link_options(bridge_cv PRIVATE -flto)
39+
# endif()
40+
41+
42+
set(BRIDGE_CV_OBJECT_FILES $<TARGET_OBJECTS:bridge_cv>)
43+
44+
45+
set(CHAI_CV_LINKER_ARGS
46+
-M ${PROJECT_ROOT_DIR}/lib
47+
${BRIDGE_DIR}/include/bridge.h
48+
${BRIDGE_CV_OBJECT_FILES}
49+
-L ${LIBTORCH_DIR}/lib
50+
${LIBTORCH_LIBS_LINKER_ARGS}
51+
--ldflags "-Wl,-rpath,${LIBTORCH_DIR}/lib"
52+
)
53+
54+
55+
56+
57+
58+
59+
60+
161
add_executable(SplitLoop
262
${CMAKE_CURRENT_SOURCE_DIR}/split_loop.chpl
363
${CHAI_LIB_FILES}
464
)
5-
add_dependencies(SplitLoop bridge)
6-
add_dependencies(SplitLoop ChAI)
65+
66+
add_dependencies(SplitLoop bridge_cv)
67+
# add_dependencies(SplitLoop ChAI)
768
target_link_options(SplitLoop
869
PRIVATE
9-
${CHAI_LINKER_ARGS}
70+
${CHAI_CV_LINKER_ARGS}
1071
)
1172

73+
cmake_print_variables(CHAI_CV_LINKER_ARGS)
74+
cmake_print_variables(OpenCV_LIBS)
75+
cmake_print_variables(ACCELERATE)
76+
cmake_print_variables(METAL)
77+
cmake_print_variables(FOUNDATION)
78+
1279
set_target_properties(SplitLoop PROPERTIES
1380
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
14-
)
81+
)
82+
83+
# if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
84+
# target_compile_options(SplitLoop PRIVATE -Ofast -flto -ffast-math)
85+
# target_link_options(SplitLoop PRIVATE -flto)
86+
# endif()

examples/split_loop/split_loop.chpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ proc main(args: [] string) {
2121
}
2222

2323

24-
24+
Bridge.showWebcam();
2525

2626
writeln("Done!");
2727
}

lib/Bridge.chpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ module Bridge {
9191

9292
extern "split_loop_filler" proc splitLoopFiller(n: int(64),ret: c_ptr(int(64))): void;
9393

94+
extern "show_webcam" proc showWebcam(): void;
95+
9496
// extern "capture_webcam_bridge" proc captureWebcam(
9597
// in cam_index: int(32)): bridge_tensor_t;
9698

0 commit comments

Comments
 (0)