gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.64k stars 2.55k forks source link

Plotting using Gradio #1871

Closed sa92leh-fs closed 2 years ago

sa92leh-fs commented 2 years ago

Describe the bug

I want to plot the output of multiple runs on the Gradio interface but since the output is not stored, I will extract the data from the log file, and plot the count but this doesn't seem to be working. My ultimate goal is to show a plot that gets updated and does not get reset every time I submit

Is there an existing issue for this?

Reproduction

from pandas.io.parsers.python_parser import count_empty_vals
import cv2
import os
import gradio as gr
from PIL import Image
import matplotlib.pyplot as plt
import pandas as pd
def crowd(vid_path, Area):
  vid = cv2.VideoCapture(vid_path)
  try:
      # creating a folder named data
      if not os.path.exists('data'):
          os.makedirs('data')
  # if not created then raise error
  except OSError:
      print('Error: Creating directory of data')
  # frame
  currentframe = 0
  while (True):
      # reading from frame
      success, frame = vid.read()
      if success:
          # continue creating images until video remains
          name = './data/frame' + str(currentframe) + '.jpg'
          print('Creating...' + name)
          # writing the extracted images
          cv2.imwrite(name, frame)
          # increasing counter so that it will
          # show how many frames are created
          currentframe += 1
          results = detect_objects(name, detector)
          count=count_persons(name, detector, threshold=0.15)
          final_img=draw_bboxes(name, results,threshold=0.15)
          density=count/Area 
          if density>=0 and density<=0.5:
            dens="Level A"
          elif density>=0.5 and density<=1:
            dens="Level B"
          elif density>=1 and density<=5:
            dens="Level C" 
          elif density>=5 and density<=10:
            dens="Level D"   
          else:
            dens="Level E"
          return final_img, dens,count
      else:
          break

def plot(file):
  df=pd.read_csv(file)
  df.columns = ['Video Path', 'Area', 'Path', 'LoS','count','flag','user','timestamp']
  df['timestamp']= df['timestamp'].astype('datetime64[ns]')
  pl=df.plot(x ='timestamp', y='count',kind = 'bar')
  myplot=plt.figure(pl)
  return myplot

plot = gr.interface.Interface(fn=plot,inputs='file',outputs='plot',
                              css="""
    .container {background-color: black; border-radius: 0%;}
    .gr-button {background-color: blue;}
    .output-markdown p{color: white; font-weight: bold; font-size: 18px;}
    """)

demo = gr.interface.Interface(fn=crowd,inputs=[gr.Video(format='mp4'),
                                               gr.inputs.Number(label="Area in square meter")],outputs=["image","text","number"],
      capture_session=True,
      interpretation="default",layout="horizontal",
    allow_flagging=True,
    live=True,
    show_error=True,
    enable_queue=True,
    description="This application tells you how dense your area is, from level A to level E where A is not dense area and E is extremely dense", 
    #css="""
    #.container {background-color: white; filter: drop-shadow(0 4px 3px rgb(0 0 0 / 0.07)) drop-shadow(0 2px 2px rgb(0 0 0 / 0.06)); border-radius: 0%;}
    #.gr-button {background-color: blue;}
    #195 p{color: blue;}
    #196{height: 70vh; min-height: 70vh;}
    #197{height: 100%; display: flex; justify-content: space-between;}
    #"""
    css="""
    .container {background-color: black; border-radius: 0%;}
    .gr-button {background-color: blue;}
    .output-markdown p{color: white; font-weight: bold; font-size: 18px;}
    """)
#gr.Parallel(demo,plot).launch(enable_queue=True,debug = 'True')
final = gr.TabbedInterface([demo, plot],["Crowd Counting","Report"])

if __name__ == "__main__":
    final.launch()

Read the video from specified path

Screenshot

image

Logs

No error is reported

System Info

Gradio interface, the latest version

Severity

annoying

sa92leh-fs commented 2 years ago

I was able to remove the error but the plot is not coming in the output box, please see below image

dawoodkhan82 commented 2 years ago

@sa92leh-fs In terms of your original issue of updating and keeping track of plots from previous runs; you can use keep track of the session state. here's more info here (https://gradio.app/interface_state/#session-state). You can achieve keeping track of the previous plot figure with these steps:

  1. Pass in an extra parameter into your function, which represents the state of the interface.
  2. At the end of the function, return the updated value of the state as an extra return value.
  3. Add the 'state' input and 'state' output components when creating your Interface

Let me know if this solves your original issue.

abidlabs commented 2 years ago

Thanks @dawoodkhan82. Going to close this issue for lack of follow up

henryxparker commented 3 months ago

This is still an issue. Here is the most bare bones example and it still displays an empty plot:

import matplotlib.pyplot as plt
from sklearn import metrics
import numpy as np

import gradio as gr

def plot_conf_mat():
    confusion_matrix = np.array([[2, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 2, 0, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 0, 1, 0, 2, 0]])

    cm_display = metrics.ConfusionMatrixDisplay(confusion_matrix = confusion_matrix, display_labels=["Mr.", "Schmitty", "Warben", "Jaegar", "Man", "Jensen"])
    cm_display.plot(xticks_rotation=85, cmap=plt.cm.Oranges)
    figure = plt.figure()

    return figure

demo = gr.Interface(
    plot_conf_mat,
    [],
    gr.Plot(),
)

if __name__ == "__main__":
    demo.launch(show_error=True)