当前位置:   article > 正文

python opencv读取网络图片_image.open(bytesio(response.content)) timeout

image.open(bytesio(response.content)) timeout

pil

  1. 方法二,使用PIL+requests:
  2. import requests
  3. from PIL import Image
  4. from io import BytesIO
  5. response = requests.get(img_src)
  6. image = Image.open(BytesIO(response.content))
  7. image.save('D:/9.jpg')

 

 

 

  1. import time
  2. import cv2
  3. import numpy as np
  4. import requests
  5. path='d:/guo.jpg'
  6. # img=cv2.imread(path)
  7. # x = img.tobytes()
  8. # # 从二进制文件到图片(numpy.ndarray):
  9. #
  10. # aaa=np.fromstring(x, np.uint8)
  11. # img = cv2.imdecode(aaa ,cv2.IMREAD_COLOR)
  12. #
  13. # cv2.imshow("1",img)
  14. # cv2.waitKeyEx()
  15. # import cv2 # opencv-python (3.4.2.16)
  16. # import numpy as np # numpy (1.14.5)
  17. #每一张图片需要600ms
  18. for i in range(10):
  19. start=time.time()
  20. file = requests.get("https://www.baidu.com/img/bd_logo1.png")
  21. img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
  22. print('time',time.time()-start)
  23. cv2.imshow("1",img)
  24. cv2.waitKeyEx()
  25. #需要1000ms左右
  26. for i in range(10):
  27. start=time.time()
  28. # file = requests.get("https://www.baidu.com/img/bd_logo1.png")
  29. url = 'http://i5.qhimg.com/t019c3e49c9c9319c33.jpg'
  30. url = 'https://www.baidu.com/img/bd_logo1.png'
  31. cap = cv2.VideoCapture(url)
  32. ret = cap.isOpened()
  33. while (ret):
  34. ret, img = cap.read()
  35. if not ret: break
  36. # img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
  37. print('time',time.time()-start)
  38. cv2.imshow('photo', img)
  39. cv2.waitKey(0)

 

c++读取:

https://answers.opencv.org/question/91344/load-image-from-url/

  1. #include "curl/curl.h" // has to go before opencv headers
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. #include <opencv2/opencv.hpp>
  6. using namespace cv;
  7. //curl writefunction to be passed as a parameter
  8. // we can't ever expect to get the whole image in one piece,
  9. // every router / hub is entitled to fragment it into parts
  10. // (like 1-8k at a time),
  11. // so insert the part at the end of our stream.
  12. size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata)
  13. {
  14. vector<uchar> *stream = (vector<uchar>*)userdata;
  15. size_t count = size * nmemb;
  16. stream->insert(stream->end(), ptr, ptr + count);
  17. return count;
  18. }
  19. //function to retrieve the image as cv::Mat data type
  20. cv::Mat curlImg(const char *img_url, int timeout=10)
  21. {
  22. vector<uchar> stream;
  23. CURL *curl = curl_easy_init();
  24. curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
  25. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // pass the writefunction
  26. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); // pass the stream ptr to the writefunction
  27. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); // timeout if curl_easy hangs,
  28. CURLcode res = curl_easy_perform(curl); // start curl
  29. curl_easy_cleanup(curl); // cleanup
  30. return imdecode(stream, -1); // 'keep-as-is'
  31. }
  32. int main(void)
  33. {
  34. Mat image = curlImg("http://www.cars.co.za/images/pictures/general/graphic_sellyourcar.png");
  35. if (image.empty())
  36. return -1; // load fail
  37. namedWindow( "Image output", CV_WINDOW_AUTOSIZE );
  38. imshow("Image output",image); // here's your car ;)
  39. waitKey(0); // infinite
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/888142
推荐阅读
相关标签
  

闽ICP备14008679号