# Projects / PersonDetection / perdet / utils / geometry.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69``` ```# encoding: utf-8 # Author: Nicholas Piël # # GeoMetry file containing conveniance functions def intersection(boxa, boxb): ''' Returns area of overlap ''' xa,ya,Xa,Ya = boxa xb,yb,Xb,Yb = boxb dx = min(Xa, Xb) - max(xa, xb) if not dx > 0: return 0 dy = min(Ya, Yb) - max(ya, yb) if not dy > 0: return 0 return dx*dy def surface(box): ''' Calculate the surface of a box ''' x,y,X,Y = box return (X-x)*(Y-y) def inside(boxa, boxb): ''' Return true when boxa is totally inside boxb ''' xa,ya,Xa,Ya = boxa xb,yb,Xb,Yb = boxb return xa > xb and Xa < Xb and ya > yb and Ya < Yb def overlap_ratio(boxa, boxb): ''' This function returns a tuple: namely its smallestbox and the overlapratio ''' overlap = intersection(boxa, boxb) if not overlap: return None, 0 surfa = surface(boxa) surfb = surface(boxb) if surfa < surfb: return boxa, (float(overlap) / surfa) else: return boxb, (float(overlap) / surfb) ####### Manipulations def multiply(box, factor): ''' Multiply the box by a certain factor seen from the center of the box ''' x,y,X,Y = box dx = (X-x)/2.0 * factor dy = (Y-y)/2.0 * factor return x-dx, y-dy, X+dx, Y+dy def create_body(facebox, width=3, height=5, shift=0): ''' Given a face box create a bady box ''' x,y,X,Y = facebox dx = (X-x)/2.0*width dy = (Y-y)/2.0 return x-dx, y+dy*shift, X+dx, Y+dy*height ```