如何将图像转换为Golang的张量?

发布于 2025-02-08 07:19:51 字数 1057 浏览 1 评论 0原文

我正在尝试将此python接口转换为tensorflow服务到Golang:

image = cv2.imread("1.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256, 256))
image = np.expand_dims(image, axis=0)
image = np.true_divide(image, 255)
np.shape(image)

我想到的是:

Package Main

import (
    "log"
    "image"
    "gocv.io/x/gocv"
)
func main() {
    imageFilePath := "./a.jpg"
    mat := gocv.IMRead(imageFilePath, gocv.IMReadAnyColor)
    if mat.Empty() {
        log.Panic("Can not read Image file : ", imageFilePath)
        return
    } 
    resizeImage := gocv.NewMat()
    gocv.Resize(mat, &resizeImage, image.Point{X: 256, Y: 256}, 0, 0, gocv.InterpolationNearestNeighbor)
 
    img := resizeImage.Clone()
    gocv.CvtColor(resizeImage, &img, gocv.ColorBGRToRGB)
    // equivalent to np.expand_dims(image, axis=0) ? 

}

,我尝试从 gonum ,但没有运气将其插入GOCV,所以我毫无头绪,感谢您对此的帮助。

I'm trying to convert this python interface to tensorflow serving into golang:

image = cv2.imread("1.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256, 256))
image = np.expand_dims(image, axis=0)
image = np.true_divide(image, 255)
np.shape(image)

What I'm came up with is:

package main

import (
    "log"
    "image"
    "gocv.io/x/gocv"
)
func main() {
    imageFilePath := "./a.jpg"
    mat := gocv.IMRead(imageFilePath, gocv.IMReadAnyColor)
    if mat.Empty() {
        log.Panic("Can not read Image file : ", imageFilePath)
        return
    } 
    resizeImage := gocv.NewMat()
    gocv.Resize(mat, &resizeImage, image.Point{X: 256, Y: 256}, 0, 0, gocv.InterpolationNearestNeighbor)
 
    img := resizeImage.Clone()
    gocv.CvtColor(resizeImage, &img, gocv.ColorBGRToRGB)
    // equivalent to np.expand_dims(image, axis=0) ? 

}

I've tried to get help from gonum but had no luck plugging it to gocv, so I'm left clueless and appreciate your help about this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文