How can i pass capture image directly as a binary data for processing in API calling (Microsoft Cognitive...












0















I am using Microsoft cognitive services for face and emotion recognition with python language.
Now first i am capturing a image from the webcam using opencv and saving that image in a folder and in API post request i am passing image address for processing then i am getting the desired output.
Now i want to save time of processing and want to capture the image from camera and send directly for processing without saving it. How can i do this using python ?
Please help me i an newbie in field of programming.



Here's my code:



while(True):
ret,img=cam.read()
faces=faceDetect.detectMultiScale(img,1.3,5)

for(x,y,w,h) in faces:
sampleNumber=sampleNumber+1
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg",img)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
cv2.waitKey(10)
cv2.imshow("Face",img)

img_filename = "C:/Users/Robot 2/Desktop/codes_msc/dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg"
with open(img_filename, 'rb') as f:
img_data = f.read()
header =
{
# Request headers for detection
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key
}



r = requests.post(api_url,
params=params,
headers=header,
data=img_data)
#Here i don't want to pass img_data as an address i just want to pass image captured









share|improve this question



























    0















    I am using Microsoft cognitive services for face and emotion recognition with python language.
    Now first i am capturing a image from the webcam using opencv and saving that image in a folder and in API post request i am passing image address for processing then i am getting the desired output.
    Now i want to save time of processing and want to capture the image from camera and send directly for processing without saving it. How can i do this using python ?
    Please help me i an newbie in field of programming.



    Here's my code:



    while(True):
    ret,img=cam.read()
    faces=faceDetect.detectMultiScale(img,1.3,5)

    for(x,y,w,h) in faces:
    sampleNumber=sampleNumber+1
    cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg",img)
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
    cv2.waitKey(10)
    cv2.imshow("Face",img)

    img_filename = "C:/Users/Robot 2/Desktop/codes_msc/dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg"
    with open(img_filename, 'rb') as f:
    img_data = f.read()
    header =
    {
    # Request headers for detection
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscription_key
    }



    r = requests.post(api_url,
    params=params,
    headers=header,
    data=img_data)
    #Here i don't want to pass img_data as an address i just want to pass image captured









    share|improve this question

























      0












      0








      0


      1






      I am using Microsoft cognitive services for face and emotion recognition with python language.
      Now first i am capturing a image from the webcam using opencv and saving that image in a folder and in API post request i am passing image address for processing then i am getting the desired output.
      Now i want to save time of processing and want to capture the image from camera and send directly for processing without saving it. How can i do this using python ?
      Please help me i an newbie in field of programming.



      Here's my code:



      while(True):
      ret,img=cam.read()
      faces=faceDetect.detectMultiScale(img,1.3,5)

      for(x,y,w,h) in faces:
      sampleNumber=sampleNumber+1
      cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg",img)
      cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
      cv2.waitKey(10)
      cv2.imshow("Face",img)

      img_filename = "C:/Users/Robot 2/Desktop/codes_msc/dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg"
      with open(img_filename, 'rb') as f:
      img_data = f.read()
      header =
      {
      # Request headers for detection
      'Content-Type': 'application/octet-stream',
      'Ocp-Apim-Subscription-Key': subscription_key
      }



      r = requests.post(api_url,
      params=params,
      headers=header,
      data=img_data)
      #Here i don't want to pass img_data as an address i just want to pass image captured









      share|improve this question














      I am using Microsoft cognitive services for face and emotion recognition with python language.
      Now first i am capturing a image from the webcam using opencv and saving that image in a folder and in API post request i am passing image address for processing then i am getting the desired output.
      Now i want to save time of processing and want to capture the image from camera and send directly for processing without saving it. How can i do this using python ?
      Please help me i an newbie in field of programming.



      Here's my code:



      while(True):
      ret,img=cam.read()
      faces=faceDetect.detectMultiScale(img,1.3,5)

      for(x,y,w,h) in faces:
      sampleNumber=sampleNumber+1
      cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg",img)
      cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
      cv2.waitKey(10)
      cv2.imshow("Face",img)

      img_filename = "C:/Users/Robot 2/Desktop/codes_msc/dataSet/User."+str(id)+"."+str(sampleNumber)+".jpg"
      with open(img_filename, 'rb') as f:
      img_data = f.read()
      header =
      {
      # Request headers for detection
      'Content-Type': 'application/octet-stream',
      'Ocp-Apim-Subscription-Key': subscription_key
      }



      r = requests.post(api_url,
      params=params,
      headers=header,
      data=img_data)
      #Here i don't want to pass img_data as an address i just want to pass image captured






      python opencv computer-vision microsoft-cognitive






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 10:47









      Akash DixitAkash Dixit

      115




      115
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can encode the image in memory with cv.imencode, and send that to the API. It would look something like the following:



          ret,buf = cv.imencode('.jpg', img)

          headers = {
          'Content-Type':'application/octet-stream',
          'Ocp-Apim-Subscription-Key':subscription_key }

          api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect'

          params = {
          'returnFaceLandmarks':True,
          'returnFaceAttributes':'emotion,age,gender' }

          r = requests.post(api_url,
          params=params,
          headers=headers,
          data=buf.tobytes())





          share|improve this answer
























          • its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

            – Akash Dixit
            Nov 22 '18 at 7:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317697%2fhow-can-i-pass-capture-image-directly-as-a-binary-data-for-processing-in-api-cal%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can encode the image in memory with cv.imencode, and send that to the API. It would look something like the following:



          ret,buf = cv.imencode('.jpg', img)

          headers = {
          'Content-Type':'application/octet-stream',
          'Ocp-Apim-Subscription-Key':subscription_key }

          api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect'

          params = {
          'returnFaceLandmarks':True,
          'returnFaceAttributes':'emotion,age,gender' }

          r = requests.post(api_url,
          params=params,
          headers=headers,
          data=buf.tobytes())





          share|improve this answer
























          • its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

            – Akash Dixit
            Nov 22 '18 at 7:24
















          0














          You can encode the image in memory with cv.imencode, and send that to the API. It would look something like the following:



          ret,buf = cv.imencode('.jpg', img)

          headers = {
          'Content-Type':'application/octet-stream',
          'Ocp-Apim-Subscription-Key':subscription_key }

          api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect'

          params = {
          'returnFaceLandmarks':True,
          'returnFaceAttributes':'emotion,age,gender' }

          r = requests.post(api_url,
          params=params,
          headers=headers,
          data=buf.tobytes())





          share|improve this answer
























          • its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

            – Akash Dixit
            Nov 22 '18 at 7:24














          0












          0








          0







          You can encode the image in memory with cv.imencode, and send that to the API. It would look something like the following:



          ret,buf = cv.imencode('.jpg', img)

          headers = {
          'Content-Type':'application/octet-stream',
          'Ocp-Apim-Subscription-Key':subscription_key }

          api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect'

          params = {
          'returnFaceLandmarks':True,
          'returnFaceAttributes':'emotion,age,gender' }

          r = requests.post(api_url,
          params=params,
          headers=headers,
          data=buf.tobytes())





          share|improve this answer













          You can encode the image in memory with cv.imencode, and send that to the API. It would look something like the following:



          ret,buf = cv.imencode('.jpg', img)

          headers = {
          'Content-Type':'application/octet-stream',
          'Ocp-Apim-Subscription-Key':subscription_key }

          api_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect'

          params = {
          'returnFaceLandmarks':True,
          'returnFaceAttributes':'emotion,age,gender' }

          r = requests.post(api_url,
          params=params,
          headers=headers,
          data=buf.tobytes())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 1:39









          cthrashcthrash

          2,4652610




          2,4652610













          • its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

            – Akash Dixit
            Nov 22 '18 at 7:24



















          • its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

            – Akash Dixit
            Nov 22 '18 at 7:24

















          its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

          – Akash Dixit
          Nov 22 '18 at 7:24





          its worked out for me success, img_data = cv2.imencode('.jpg', img) img_data = img_data.tobytes()

          – Akash Dixit
          Nov 22 '18 at 7:24




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317697%2fhow-can-i-pass-capture-image-directly-as-a-binary-data-for-processing-in-api-cal%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)