Python Forum
Beginner: Exporting variable/array using OSC
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner: Exporting variable/array using OSC
#1
Hi guys

I'm very new to Python and I currently just need to get by this problem for an upcoming project deadline, I'll learn more about Python after my project. I'm working on a graphics interface in Processing and want to receive the 'probability' values that are output from a facial recognition algorithm. I'm currently having issues storing the probability data as a 7-value float vector/array and sending this data to a port. I need to export these 7 probability values at every frame as a float vector to port 8388 using OSC. I've imported the pythonOSC library but don't know how to store the probability values and send them on this port.. Can someone guide me through the steps or write the code for me? Would appreciate it. Please find the code below. I need the values of 'prob'. Full github repo where I took code from: https://github.com/omar178/Emotion-recognition#p1

import argparse
import random
import time

from pythonosc import udp_client

#FACE RECOG PROGRAM FROM GITHUB REPO
from keras.preprocessing.image import img_to_array
import imutils
import cv2
from keras.models import load_model
import numpy as np

# parameters for loading data and images
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'

# hyper-parameters for bounding boxes shape
# loading models
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
 "neutral"]


#feelings_faces = []
#for index, emotion in enumerate(EMOTIONS):
   # feelings_faces.append(cv2.imread('emojis/' + emotion + '.png', -1))

# starting video streaming
cv2.namedWindow('your_face')
camera = cv2.VideoCapture(0)
while True:
    frame = camera.read()[1]
    #reading the frame
    frame = imutils.resize(frame,width=300)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
    
    canvas = np.zeros((250, 300, 3), dtype="uint8")
    frameClone = frame.copy()
    if len(faces) > 0:
        faces = sorted(faces, reverse=True,
        key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        (fX, fY, fW, fH) = faces
                    # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
            # the ROI for classification via the CNN
        roi = gray[fY:fY + fH, fX:fX + fW]
        roi = cv2.resize(roi, (64, 64))
        roi = roi.astype("float") / 255.0
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis=0)
        
        
        preds = emotion_classifier.predict(roi)[0]
        emotion_probability = np.max(preds)
        label = EMOTIONS[preds.argmax()]
    else: continue
    
    for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
        #I NEED THESE PROBABILITY VALUES SENT, PILE 7 VALUES FOR THE DIFFERENT EMOTIONS TOGETHER AT A TIME AND SEND.
                print(prob)
		# construct the label text
                text = "{}: {:.2f}%".format(emotion, prob * 100)

                # draw the label + probability bar on the canvas
               # emoji_face = feelings_faces[np.argmax(preds)]

                
                w = int(prob * 300)
                cv2.rectangle(canvas, (7, (i * 35) + 5),
                (w, (i * 35) + 35), (0, 0, 255), -1)
                cv2.putText(canvas, text, (10, (i * 35) + 23),
                cv2.FONT_HERSHEY_SIMPLEX, 0.45,
                (255, 255, 255), 2)
                cv2.putText(frameClone, label, (fX, fY - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
                cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
                             (0, 0, 255), 2)
     
#    for c in range(0, 3):
#        frame[200:320, 10:130, c] = emoji_face[:, :, c] * \
#        (emoji_face[:, :, 3] / 255.0) + frame[200:320,
#        10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0)


    cv2.imshow('your_face', frameClone)
    cv2.imshow("Probabilities", canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
	      break

camera.release()
cv2.destroyAllWindows()
Reply
#2
So basically you took someone else code and want someone else to write the rest for you. And you promise to learn, in the distant future...
I don't think it will work...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Mate, I literally said that I'm not able to dive into it for a reason - I appreciate your seemingly negative criticism but I've written a whole lot of complex code in Processing already relating to this project. I've built a three dimensional face that interpolates between values to result in facial expressions. As an additional tool I even built a face detection algorithm based on mapping vertices, but this wasn't quite robust enough - so yes, there's no harm in using someone else's Machine Learning algorithm to get better results that I can input into Processing. I'm only struggling with exporting values as I've never dealt with server-client information before and I have a deadline on Thursday.

Rather than meaninglessly criticize, perhaps don't comment at all. Thanks.
Reply
#4
https://python-forum.io/misc.php?action=help&hid=52

So far we have seen no effort, just a link to someone else's code on github. Your deadline is not our concern.
Please, don't get me wrong - I don't mind using/get inspired by someone else's code.
Actually, recently there was nice article on StackOverflow's blog:

Good coders borrow, great coders steal
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
My code sends probability floats individually. I want to package this into float arrays that i could send across of size 7. How would i do this? I was thinking of using a list, appending the probabilities to it and sending a float of the list but how do you create a new list every 7 iterations of the for loop?

  while True:
#other code
for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
                client.send_message("/fil", float(prob))
#other code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create new variable that sums the array prasanthbab1234 3 2,340 Sep-26-2020, 02:03 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020