|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package vaccel |
| 4 | + |
| 5 | +/* |
| 6 | +#cgo pkg-config: vaccel |
| 7 | +#cgo LDFLAGS: -lvaccel -ldl |
| 8 | +#include <vaccel.h> |
| 9 | +
|
| 10 | +*/ |
| 11 | +import "C" |
| 12 | +import ( |
| 13 | + "fmt" |
| 14 | + "os" |
| 15 | + "unsafe" |
| 16 | +) |
| 17 | + |
| 18 | +func ImageDetectionFromFile(sess *Session, imagePath string) (string, int) { |
| 19 | + |
| 20 | + imageBytes, err := os.ReadFile(imagePath) |
| 21 | + if err != nil { |
| 22 | + fmt.Printf("Error reading file: %s\n", err) |
| 23 | + os.Exit(1) |
| 24 | + } |
| 25 | + |
| 26 | + cImageBytes := (*C.uchar)(&imageBytes[0]) |
| 27 | + cImgBuf := unsafe.Pointer(cImageBytes) |
| 28 | + cImgLen := C.size_t(len(imageBytes)) |
| 29 | + |
| 30 | + cOutImageName := (*C.uchar)(C.malloc(C.size_t(256))) |
| 31 | + |
| 32 | + /* Free the memory when done */ |
| 33 | + defer C.free(unsafe.Pointer(cOutImageName)) |
| 34 | + |
| 35 | + cRet := C.vaccel_image_detection( |
| 36 | + sess.cSess, cImgBuf, cOutImageName, |
| 37 | + cImgLen, C.size_t(256)) //nolint:gocritic |
| 38 | + |
| 39 | + var golangOut string |
| 40 | + |
| 41 | + if int(cRet) == 0 { |
| 42 | + ptr := unsafe.Pointer(cOutImageName) |
| 43 | + typeCast := (*C.char)(ptr) |
| 44 | + golangOut = C.GoString(typeCast) |
| 45 | + } else { |
| 46 | + golangOut = |
| 47 | + "A problem occurred while running the Operation" |
| 48 | + } |
| 49 | + |
| 50 | + return golangOut, int(cRet) |
| 51 | +} |
| 52 | + |
| 53 | +func ImageDetection(sess *Session, image []byte) (string, int) { |
| 54 | + |
| 55 | + cImageBytes := (*C.uchar)(&image[0]) |
| 56 | + cImgBuf := unsafe.Pointer(cImageBytes) |
| 57 | + cImgLen := C.size_t(len(image)) |
| 58 | + |
| 59 | + cOutImageName := (*C.uchar)(C.malloc(C.size_t(256))) |
| 60 | + |
| 61 | + /* Free the memory when done */ |
| 62 | + defer C.free(unsafe.Pointer(cOutImageName)) |
| 63 | + |
| 64 | + cRet := C.vaccel_image_detection( |
| 65 | + sess.cSess, cImgBuf, cOutImageName, |
| 66 | + cImgLen, C.size_t(256)) //nolint:gocritic |
| 67 | + |
| 68 | + var golangOut string |
| 69 | + |
| 70 | + if int(cRet) == 0 { |
| 71 | + ptr := unsafe.Pointer(cOutImageName) |
| 72 | + typeCast := (*C.char)(ptr) |
| 73 | + golangOut = C.GoString(typeCast) |
| 74 | + } else { |
| 75 | + golangOut = |
| 76 | + "A problem occurred while running the Operation" |
| 77 | + } |
| 78 | + |
| 79 | + return golangOut, int(cRet) |
| 80 | +} |
0 commit comments