This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
thresh=100 | |
p1,p2,d=find_closest(dist,num,thresh) | |
df = pd.DataFrame({"p1":p1,"p2":p2,"dist":d}) | |
df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%time | |
def find_closest(dist,num,thresh): | |
p1=[] | |
p2=[] | |
d=[] | |
for i in range(num): | |
for j in range(i,num): | |
if( (i!=j) & (dist[i][j]<=thresh)): | |
p1.append(i) | |
p2.append(j) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%time | |
from scipy.spatial import distance | |
def compute_distance(midpoints,num): | |
dist = np.zeros((num,num)) | |
for i in range(num): | |
for j in range(i+1,num): | |
if i!=j: | |
dst = distance.euclidean(midpoints[i], midpoints[j]) | |
dist[i][j]=dst | |
return dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#call the function | |
midpoints = [mid_point(img,person,i) for i in range(len(person))] | |
#visualize image | |
plt.figure(figsize=(20,10)) | |
plt.imshow(img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define a function which return the bottom center of every bbox | |
def mid_point(img,person,idx): | |
#get the coordinates | |
x1,y1,x2,y2 = person[idx] | |
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 2) | |
#compute bottom center of bbox | |
x_mid = int((x1+x2)/2) | |
y_mid = int(y2) | |
mid = (x_mid,y_mid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compute center | |
x_center = int((x1+x2)/2) | |
y_center = int(y2) | |
center = (x_center, y_center) | |
_ = cv2.circle(img, center, 5, (255, 0, 0), -1) | |
plt.figure(figsize=(20,10)) | |
plt.imshow(img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img = cv2.imread('frames/30.png') | |
_ = cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2) | |
plt.figure(figsize=(20,10)) | |
plt.imshow(img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#identity only persons | |
ind = np.where(classes==0)[0] | |
#identify bounding box of only persons | |
person=bbox[ind] | |
#total no. of persons | |
num= len(person) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use `Visualizer` to draw the predictions on the image. | |
v = Visualizer(img[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2) | |
v = v.draw_instance_predictions(outputs["instances"].to("cpu")) | |
cv2_imshow(v.get_image()[:, :, ::-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cfg = get_cfg() | |
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library | |
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_C4_3x.yaml")) | |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.9 # set threshold for this model | |
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well | |
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Detection/faster_rcnn_R_50_C4_3x.yaml") | |
predictor = DefaultPredictor(cfg) |