How to send OpenCV Image using python requests to Go Endpoint

huangapple go评论128阅读模式
英文:

How to send OpenCV Image using python requests to Go Endpoint

问题

这是我的相机脚本的代码:

  1. import cv2
  2. import requests
  3. from datetime import datetime
  4. from time import sleep
  5. def sendImage(frame):
  6. imencoded = cv2.imencode(".jpg", frame)[1]
  7. now = datetime.now()
  8. seq = now.strftime("%Y%m%d%H%M%S")
  9. file = {'file': (seq+'.jpg', imencoded.tobytes(), 'image/jpeg')}
  10. response = requests.post("http://localhost:3004/", files=file, timeout=5)
  11. return response
  12. def takeImage():
  13. cap = cv2.VideoCapture(0)
  14. ret, frame = cap.read()
  15. print(sendImage(frame))
  16. cap.release()
  17. while 1:
  18. takeImage()
  19. sleep(5)

这是我的Go服务器的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/gorilla/mux"
  7. )
  8. func imgHandler(w http.ResponseWriter, r *http.Request) {
  9. fmt.Println("收到请求!")
  10. r.ParseMultipartForm(10 << 20)
  11. file, handler, err := r.FormFile("myFile")
  12. if err != nil {
  13. fmt.Println("错误!")
  14. return
  15. }
  16. defer file.Close()
  17. fmt.Println(handler.Filename)
  18. }
  19. func getHandler(w http.ResponseWriter, r *http.Request) {
  20. fmt.Fprintf(w, "Hello World API!")
  21. }
  22. func main() {
  23. r := mux.NewRouter()
  24. r.HandleFunc("/", imgHandler).Methods("POST")
  25. r.HandleFunc("/", getHandler).Methods("GET")
  26. http.Handle("/", r)
  27. log.Fatal(http.ListenAndServe(":3004", nil))
  28. }

我不知道为什么在FormFile函数上一直出错。我的最终目标是建立一个安全的连接到一个端点,这样我就可以从我的树莓派发送图像到我的服务器并将其保存在本地。我该如何使用Python的requests库将文件发送到我的Go端点?我已经看到了一些解决方案,涉及在HTML页面上使用<form>元素,这种方法是可行的。

英文:

Here is the code for my camera script

  1. import cv2
  2. import requests
  3. from datetime import datetime
  4. from time import sleep
  5. def sendImage(frame):
  6. imencoded = cv2.imencode(&quot;.jpg&quot;, frame)[1]
  7. now = datetime.now()
  8. seq = now.strftime(&quot;%Y%m%d%H%M%S&quot;)
  9. file = {&#39;file&#39;: (seq+&#39;.jpg&#39;, imencoded.tobytes(), &#39;image/jpeg&#39;)}
  10. response = requests.post(&quot;http://localhost:3004/&quot;, files=file, timeout=5)
  11. return response
  12. def takeImage():
  13. cap = cv2.VideoCapture(0)
  14. ret, frame = cap.read()
  15. print(sendImage(frame))
  16. cap.release()
  17. while 1:
  18. takeImage()
  19. sleep(5)

and my Go Server

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;log&quot;
  5. &quot;net/http&quot;
  6. &quot;github.com/gorilla/mux&quot;
  7. )
  8. func imgHandler(w http.ResponseWriter, r *http.Request) {
  9. fmt.Println(&quot;recieved request!&quot;)
  10. r.ParseMultipartForm(10 &lt;&lt; 20)
  11. file, handler, err := r.FormFile(&quot;myFile&quot;)
  12. if err != nil {
  13. fmt.Println(&quot;error!&quot;)
  14. return
  15. }
  16. defer file.Close()
  17. fmt.Println(handler.Filename)
  18. }
  19. func getHandler(w http.ResponseWriter, r *http.Request) {
  20. fmt.Fprintf(w, &quot;Hello World API!&quot;)
  21. }
  22. func main() {
  23. r := mux.NewRouter()
  24. r.HandleFunc(&quot;/&quot;, imgHandler).Methods(&quot;POST&quot;)
  25. r.HandleFunc(&quot;/&quot;, getHandler).Methods(&quot;GET&quot;)
  26. http.Handle(&quot;/&quot;, r)
  27. log.Fatal(http.ListenAndServe(&quot;:3004&quot;, nil))
  28. }

I have no idea why I keep on getting an error on my FormFile function. My end goal is to have a secure connection to an endpoint so that I can send images from my raspberry pi to my server and have it saved locally. How can I do this so I send files to my Go endpoint using the python requests library. I've already seen solutions that involve using <form> elements on a html page that works.

答案1

得分: 1

在Python中,你调用的字段是file,而在Go中,你尝试访问的是myFile。对Go代码的更改如下:

  1. file, handler, err := r.FormFile("file")

为了找出问题,我将调试行更改为打印错误信息:

  1. if err != nil {
  2. fmt.Printf("error: %s\n", err)
  3. return
  4. }

(通常在服务器中使用log.Printf How to send OpenCV Image using python requests to Go Endpoint

英文:

In Python you call the field file where in Go you try to access myFile. The change to the Go code was:

  1. file, handler, err := r.FormFile(&quot;file&quot;)

To find this out, I've changed the debug line to print the error as well:

  1. if err != nil {
  2. fmt.Printf(&quot;error: %s\n&quot;, err)
  3. return
  4. }

(In general, use log.Printf in servers How to send OpenCV Image using python requests to Go Endpoint

huangapple
  • 本文由 发表于 2022年8月8日 10:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/73272374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定