Reputation: 117
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
Upvotes: 0
Views: 1361
Reputation: 2973
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())
Upvotes: 2