Skip to content

Commit bd2f39a

Browse files
committed
Implement windows version to get camera devices in bindings
1 parent ce2f1ff commit bd2f39a

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

openvino_bindings/src/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cc_library(
55
deps = [
66
"@nlohmann_json//:json",
77
"//third_party:opencv",
8+
"//src/utils:input_devices",
89
"//src/utils:status",
910
"//src/utils:utils",
1011
"//src/image:image_inference",

openvino_bindings/src/bindings.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "src/vlm/vlm_inference.h"
2121
#include "src/utils/errors.h"
2222
#include "src/utils/utils.h"
23+
#include "src/utils/input_devices.h"
2324
#include "src/utils/status.h"
2425
#include "src/image/json_serialization.h"
2526
#include "src/image/csv_serialization.h"
@@ -501,6 +502,28 @@ StatusOrDevices* getAvailableDevices() {
501502
return new StatusOrDevices{OkStatus, "", devices, (int)device_ids.size() + 1};
502503
}
503504

505+
StatusOrInputDevices* getAvailableCameraDevices() {
506+
auto cameras = list_camera_devices();
507+
InputDevice* devices = new InputDevice[cameras.size()];
508+
int i = 0;
509+
for (auto camera: cameras) {
510+
devices[i] = { (int)camera.first, strdup(camera.second.c_str()) };
511+
i++;
512+
}
513+
514+
return new StatusOrInputDevices{OkStatus, "", devices, (int)cameras.size()};
515+
}
516+
517+
StatusOrString* pdfExtractText(const char* pdf_path) {
518+
try {
519+
auto output = sentence_extractor::extract_text_from_pdf(pdf_path);
520+
return new StatusOrString{OkStatus, "", strdup(output.c_str())};
521+
} catch (...) {
522+
auto except = handle_exceptions();
523+
return new StatusOrString{except->status, except->message};
524+
}
525+
}
526+
504527
Status* handle_exceptions() {
505528
try {
506529
throw;

openvino_bindings/src/bindings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ typedef struct {
3636
const char* name;
3737
} Device;
3838

39+
typedef struct {
40+
int id;
41+
const char* name;
42+
} InputDevice;
43+
3944
typedef struct {
4045
float start_ts;
4146
float end_ts;
@@ -138,6 +143,13 @@ typedef struct {
138143
int size;
139144
} StatusOrDevices;
140145

146+
typedef struct {
147+
enum StatusEnum status;
148+
const char* message;
149+
InputDevice* value;
150+
int size;
151+
} StatusOrInputDevices;
152+
141153
typedef void (*ImageInferenceCallbackFunction)(StatusOrString*);
142154
typedef void (*LLMInferenceCallbackFunction)(StatusOrString*);
143155
typedef void (*VLMInferenceCallbackFunction)(StatusOrString*);
@@ -196,6 +208,8 @@ EXPORT StatusOrInt* speechToTextVideoDuration(CSpeechToText instance);
196208
EXPORT StatusOrWhisperModelResponse* speechToTextTranscribe(CSpeechToText instance, int start, int duration, const char* language);
197209

198210
EXPORT StatusOrDevices* getAvailableDevices();
211+
EXPORT StatusOrInputDevices* getAvailableCameraDevices();
212+
199213
Status* handle_exceptions();
200214

201215
//extern "C" void report_rss();

openvino_bindings/src/utils/BUILD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ cc_library(
1717
],
1818
)
1919

20+
cc_library(
21+
name = "input_devices",
22+
hdrs = ["input_devices.h"],
23+
deps = [
24+
":errors",
25+
],
26+
linkopts = [
27+
"mfplat.lib",
28+
"mf.lib",
29+
"mfuuid.lib",
30+
"ole32.lib",
31+
],
32+
)
33+
2034
cc_library(
2135
name = "metrics",
2236
hdrs = [
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#ifndef WINDOWS_INPUT_DEVICES_H_
2+
#define WINDOWS_INPUT_DEVICES_H_
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <map>
7+
#include <string>
8+
9+
#include "status.h"
10+
#include "errors.h"
11+
12+
13+
#if _WIN32
14+
#include <windows.h>
15+
#include <mfapi.h>
16+
#include <mfidl.h>
17+
#include <mfobjects.h>
18+
#include <mferror.h>
19+
#pragma comment(lib, "mfplat.lib")
20+
#pragma comment(lib, "mf.lib")
21+
22+
std::map<size_t, std::string> list_camera_devices() {
23+
HRESULT hr = MFStartup(MF_VERSION);
24+
if (FAILED(hr)) {
25+
throw api_error(InputDeviceError, "MFStartup failed.");
26+
}
27+
28+
IMFAttributes* pAttributes = nullptr;
29+
hr = MFCreateAttributes(&pAttributes, 1);
30+
if (FAILED(hr)) {
31+
MFShutdown();
32+
throw api_error(InputDeviceError, "MFCreateAttributes failed.");
33+
}
34+
35+
// Specify that we want video capture devices
36+
hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
37+
if (FAILED(hr)) {
38+
pAttributes->Release();
39+
MFShutdown();
40+
throw api_error(InputDeviceError, "SetGUID failed.");
41+
}
42+
43+
IMFActivate** ppDevices = nullptr;
44+
UINT32 count = 0;
45+
46+
47+
std::map<size_t, std::string> cameras = {};
48+
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
49+
if (SUCCEEDED(hr)) {
50+
for (UINT32 i = 0; i < count; i++) {
51+
WCHAR* szFriendlyName = nullptr;
52+
UINT32 cchName = 0;
53+
hr = ppDevices[i]->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &szFriendlyName, &cchName);
54+
if (SUCCEEDED(hr)) {
55+
std::wstring ws(szFriendlyName);
56+
cameras.insert({i, std::string(ws.begin(), ws.end())});
57+
//std::wcout << L"[" << i << L"]: " << szFriendlyName << std::endl;
58+
CoTaskMemFree(szFriendlyName);
59+
}
60+
ppDevices[i]->Release();
61+
}
62+
CoTaskMemFree(ppDevices);
63+
} else {
64+
std::cerr << "No camera devices found.\n";
65+
}
66+
67+
pAttributes->Release();
68+
MFShutdown();
69+
70+
return cameras;
71+
}
72+
#elif __APPLE__
73+
std::map<size_t, std::string> list_camera_devices() {
74+
return {};
75+
}
76+
#elif __linux__
77+
std::map<size_t, std::string> list_camera_devices() {
78+
return {};
79+
}
80+
#else
81+
std::map<size_t, std::string> list_camera_devices() {
82+
throw api_error(InputDeviceError, "Unsupported platform.");
83+
}
84+
#endif
85+
86+
#endif // WINDOWS_INPUT_DEVICES_H_

openvino_bindings/src/utils/status.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ enum StatusEnum {
3535
SpeechToTextFileNotOpened = -81,
3636
SpeechToTextChunkHasNoData = -82,
3737
SpeechToTextChunkOutOfBounds = -83,
38+
39+
InputDeviceError = -90,
3840
};
3941

4042
#endif // STATUS_H_

0 commit comments

Comments
 (0)