Python Forum
parse json output to simple text or variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parse json output to simple text or variable
#1
Hi,
I have below python code for face_recognition.basically it is comparing two images and providing match or not.I am getting output in json format as below.How to parse this json output to normal text or storing into a variable and display the out like thanks in advance for your response
present output:
{
"Not matched": [],
"Matched": [
[
"anil1_kZJfP0k.JPG",
{
"Matching Percentage": "68.59"
}
]
]
}
Expected output:
instead of all the brackets simple text output like:
response ={matched}
matching percentage ={68%}

import face_recognition
import os


def match(lis, img):
    images = lis
    image_to_be_matched = face_recognition.load_image_file(img)
    # encoded the loaded image into a feature vector
    image_to_be_matched_encoded = face_recognition.face_encodings(
        image_to_be_matched)[0]

    # iterate over each image
    resp = {"Matched": [], "Not matched": []}
    for image in images:
        print(images)
        #image = os.path.join(path, image)
        current_image = face_recognition.load_image_file(image)
        # encode the loaded image into a feature vector
        current_image_encoded = face_recognition.face_encodings(current_image)[0]
        # match your image with the image and check if it matches
        result = face_recognition.compare_faces(
            [image_to_be_matched_encoded], current_image_encoded)
        # check if it was a match
        face_distances = face_recognition.face_distance([image_to_be_matched_encoded], current_image_encoded)
        if result[0] == True:
            resp["Matched"].append([os.path.basename(image), {'Matching Percentage': str((1-face_distances[0])*100)[:5]}])
        else:
            resp["Not matched"].append([os.path.basename(image), {'Matching Percentage': str((1-face_distances[0])*100)[:5]}])
    for image in images:
        os.remove(image)
    os.remove(img)
    print(resp)
    return resp
Reply
#2
This is just mental exercise about how to extract data using indices and keys:

data = { 
... "Not matched": [], 
... "Matched": [ 
... [ 
... "anil1_kZJfP0k.JPG", 
... { 
... "Matching Percentage": "68.59" 
... } 
... ] 
... ] 
... } 
>>> f'response = {data["Matched"][0][0]}'
'response = anil1_kZJfP0k.JPG'
>>> f'matching percentage = {data["Matched"][0][1]["Matching Percentage"]}'
'matching percentage = 68.59'  
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parse Nested JSON String in Python rwalde 4 2,859 Sep-08-2022, 10:32 AM
Last Post: rwalde
Information Parssing Json.dump from PYTHON to PHP for output on browser jodqueshiva 1 2,372 Nov-01-2021, 02:34 PM
Last Post: snippsat
  Predicting an output variable with sklearn Ccross1 1 2,484 Jun-04-2019, 03:11 PM
Last Post: michalmonday
  Simple pendulum with variable length Gaelou 1 3,336 Aug-30-2018, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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