vchiang001 commited on
Commit
d581ff8
β€’
1 Parent(s): 9f1c007

consolidating codes

Browse files
Files changed (1) hide show
  1. app.py +77 -65
app.py CHANGED
@@ -1,72 +1,84 @@
1
  # Copied megadetector section from https://huggingface.co/spaces/hlydecker/MegaDetector_v5
 
2
 
3
- # %%
4
- #all imports
5
  import gradio as gr
6
- import torch
7
- import torchvision
 
8
  import numpy as np
9
  from PIL import Image
10
- import matplotlib.pyplot as plt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # %%
13
- # Loads a model from github repo, but you need to have the model
14
- model = torch.hub.load('ultralytics/yolov5', 'custom', "https://huggingface.co/spaces/vchiang001/MegaDetector_DLC_pose/blob/main/md_v5b.0.0.pt", force_reload=True)
15
-
16
- # %%
17
-
18
- #not sure if we need to resize... maybe just the origin image and see?
19
- """ def yolo(im): #size=640):
20
- g = (size / max(im.size)) # gain
21
- im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
22
-
23
- model = torch.hub.load('ultralytics/yolov5', 'custom', "https://huggingface.co/spaces/vchiang001/MegaDetector_DLC_pose/blob/main/md_v5b.0.0.pt", force_reload=True)
24
-
25
- results = model(im) # inference
26
- results.render() # updates results.imgs with boxes and labels
27
- return Image.fromarray(results.imgs[0]) """
28
-
29
- def yolo(im): #size=640):
30
- model = torch.hub.load('ultralytics/yolov5', 'custom', "https://huggingface.co/spaces/vchiang001/MegaDetector_DLC_pose/blob/main/md_v5b.0.0.pt", force_reload=True)
31
- results = model(im) # inference
32
- results.render() # updates results.imgs with boxes and labels
33
- return Image.fromarray(results.imgs[0])
34
-
35
- # %% dlc-live function
36
- def dlc(im):
37
- from dlclive import DLCLive, Processor
38
- imarray = np.asarray(im)
39
- dlc_proc = Processor()
40
- dlc_live = DLCLive("model/", processor=dlc_proc)
41
- dlc_live.init_inference(imarray)
42
- keypts = dlc_live.get_pose(imarray)
43
- return Image.fromarray(keypts)
44
- """
45
- from dlclive import DLCLive, Processor
46
- imarray = np.asarray(im)
47
  dlc_proc = Processor()
48
- dlc_live = DLCLive("model/", processor=dlc_proc)
49
- dlc_live.init_inference(imarray)
50
- keypts = dlc_live.get_pose(imarray)
51
- plt.imshow(im)
52
- plt.scatter(keypts[:,0], keypts[:,1], 40, color='cyan')
53
- #return Image.fromarray(results.imgs[0])
54
- """
55
- #return plt.show()
56
- #return plt.imshow(im)
57
-
58
- # %%
59
- #inputs = [image, chosen_model, size]
60
- #this is where you show the image as interface
61
- inputs = gr.inputs.Image(type="pil", label="Input Image")
62
- outputs = gr.outputs.Image(type="pil", label="Output Image")
63
-
64
-
65
- # %%
66
- title = "MegaDetector with DeepLabCut pose estimation"
67
- description = "Detect and identify animals, people and vehicles in camera trap images followed by generating poses for humans and animals"
68
- article = "<p style='text-align: center'>Detect and identify animals, people and vehicles in camera trap images followed by generating poses for humans and animals</a></p>"
69
-
70
- # %%
71
- #running the actual
72
- gr.Interface(dlc, inputs, outputs, title=title, description=description, article=article, theme="huggingface").launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Copied megadetector section from https://huggingface.co/spaces/hlydecker/MegaDetector_v5
2
+ # Copied from https://huggingface.co/spaces/Neslihan/megadetector_dlcmodels/blob/main/app.py
3
 
4
+ print("before import")
 
5
  import gradio as gr
6
+ import json
7
+ import os
8
+ import matplotlib.pyplot as plt
9
  import numpy as np
10
  from PIL import Image
11
+ from dlclive import DLCLive, Processor
12
+ from numpy import savetxt
13
+ import PIL
14
+ print("after import")
15
+
16
+ # A method that allows using dlc live, but with different models, saves poses, and plots the poses onto image
17
+ def dlclive_pose(model, crop_np, crop, index,dlc_proc):
18
+ dlc_live = DLCLive(model, processor=dlc_proc)
19
+ dlc_live.init_inference(crop_np)
20
+ keypts = dlc_live.get_pose(crop_np)
21
+ xpose = []
22
+ ypose = []
23
+ for key in keypts[:,2]:
24
+ # if key > 0.05: # which value do we need here?
25
+ i = np.where(keypts[:,2]==key)
26
+ xpose.append(keypts[i,0])
27
+ ypose.append(keypts[i,1])
28
+ plt.imshow(crop)
29
+ plt.scatter(xpose[:], ypose[:], 40, color='cyan')
30
+
31
+ canvas = plt.gca().figure.canvas
32
+ canvas.draw()
33
+ image = PIL.Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
34
+
35
+ plt.clf()
36
+ return image
37
+
38
+ def classify_image(img, file):
39
+
40
+ primate_face_model = 'model_weights/DLC_FacialLandmarks_resnet_50_iteration-1_shuffle-1'
41
+ human_model = 'model_weights/DLC_human_dancing_resnet_101_iteration-0_shuffle-1'
42
+
43
+ with open(file.name, 'r') as f:
44
+ detection_results = json.load(f)
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  dlc_proc = Processor()
47
+
48
+ # Assuming there is only 1 detection on the output
49
+ img_data = detection_results["images"][0]
50
+
51
+ output_images = []
52
+
53
+ for detections_dict in img_data["detections"]:
54
+ index = img_data["detections"].index(detections_dict)
55
+ if detections_dict["conf"] > 0.8:
56
+ x1, y1,w_box, h_box = detections_dict["bbox"]
57
+ ymin,xmin,ymax, xmax = y1, x1, y1 + h_box, x1 + w_box
58
+
59
+ imageWidth=img.size[0]
60
+ imageHeight= img.size[1]
61
+ area = (xmin * imageWidth, ymin * imageHeight, xmax * imageWidth,
62
+ ymax * imageHeight)
63
+ crop = img.crop(area)
64
+ crop_np = np.asarray(crop)
65
+
66
+ if detections_dict["category"] == "1":
67
+ selected_model = primate_face_model
68
+ elif detections_dict["category"] == "2":
69
+ selected_model = human_model
70
+
71
+ # Until we know how to dynamically add output element to gradio, just return the first image
72
+ output_images.append(dlclive_pose(selected_model, crop_np, crop, index, dlc_proc))
73
+
74
+ return output_images[0], output_images[1] # lol
75
+
76
+ input_image = gr.inputs.Image(type="pil", label="Input Image")
77
+ input_file = gr.inputs.File(label="output.json")
78
+
79
+ # Fake it till we make it, we know our example has 2 outputs
80
+ outputs = [gr.outputs.Image(type="pil", label="Output Image"), gr.outputs.Image(type="pil", label="Output Image")]
81
+
82
+ gr.Interface(fn=classify_image, inputs=[input_image, input_file], outputs=outputs, theme="huggingface").launch()
83
+
84
+