From d889de1824d70e90b2b1e2c51120517b8fbafea7 Mon Sep 17 00:00:00 2001 From: Jon Whiteaker Date: Wed, 15 Feb 2012 16:51:17 -0800 Subject: face-detect and faces.py updates --- data/face-test.py | 54 +++++++++++++++++++ data/face-train.py | 40 ++++++++++++++ data/faces.py | 27 +++++----- data/multipart.py | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 255 insertions(+), 15 deletions(-) create mode 100755 data/face-test.py mode change 100644 => 100755 data/face-train.py create mode 100755 data/multipart.py diff --git a/data/face-test.py b/data/face-test.py new file mode 100755 index 0000000..a987c49 --- /dev/null +++ b/data/face-test.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +from subprocess import Popen +import os +import sys +from face_client import FaceClient + +api_key = '34a84a7835bf24df2d84b4bded84e838' +api_secret = '5bc9e8c5a9e3a2d916abbe3659a1b3f8' +url_base = 'http://74.95.195.225/static/img/jon/users/' +client = FaceClient(api_key, api_secret) + +def test(ns,dataset,dirs): + f = open('test.log', 'w') + for d in dirs: + tids = [] + nm = 0 + nf = 0 + fp = 0 + count = 0 + user = d.rstrip('/').split('/')[-1] + for line in open(d+'/'+dataset+'-test', 'r'): + pic = line.split(',')[-2].split('/')[-1].split('.')[0]+'.jpeg' + if pic != "face.jpeg": + count += 1 + url = url_base+user+'/test/'+pic + try: + response = client.faces_recognize('all',url,namespace=ns) + sys.stderr.write(str(response)+"\n") + for photo in response['photos']: + if len(photo['tags']) > 0: + if len(photo['tags'][0]['uids']) > 0: + out = [user, pic] + for tag in photo['tags'][0]['uids']: + out += [tag['uid'], str(tag['confidence'])] + f.write("\t".join(out)+"\n") + if photo['tags'][0]['uids'][0]['uid'] != user+'@'+ns: + fp += 1 + else: + f.write("\t".join([user, pic, "no-match", "1"])+"/n") + nm += 1 + else: + f.write("\t".join([user, pic, "no-face", "0"])+"/n") + nf += 1 + except: + sys.stderr.write("Unexpexted error:"+str(sys.exc_info()[0])) + pass + print ','.join([user,str(count),str(nf),str(nm),str(fp)]) + + + +if __name__ == "__main__": + test(sys.argv[1],sys.argv[2],sys.argv[3:]) + + diff --git a/data/face-train.py b/data/face-train.py old mode 100644 new mode 100755 index 636607f..80ca4fa --- a/data/face-train.py +++ b/data/face-train.py @@ -8,3 +8,43 @@ api_key = '34a84a7835bf24df2d84b4bded84e838' api_secret = '5bc9e8c5a9e3a2d916abbe3659a1b3f8' url_base = 'http://74.95.195.225/static/img/jon/users/' client = FaceClient(api_key, api_secret) + +def train(ns,dataset,dirs): + f = open('train.log', 'w') + for d in dirs: + tids = [] + count = 0 + user = d.rstrip('/').split('/')[-1] + for line in open(d+'/'+dataset+'-train', 'r'): + pic = line.split(',')[-2].split('/')[-1].split('.')[0]+'.jpeg' + if pic != "face.jpeg": + count += 1 + url = url_base+user+'/train/'+pic + try: + response = client.faces_detect(url) + sys.stderr.write(str(response['photos'][0]['tags'])+"\n") + for photo in response['photos']: + if len(photo['tags']) > 0: + tids += [photo['tags'][0]['tid']] + f.write("\t".join([user, pic, "face"])+"/n") + else: + f.write("\t".join([user, pic, "no-face"])+"/n") + except: + sys.stderr.write("Unexpexted error:"+str(sys.exc_info()[0])) + pass + saved = client.tags_save(tids = ',' . join(tids), uid = user+'@'+ns) + sys.stderr.write(str(saved)+"\n") + trained = client.faces_train(user+'@'+ns) + sys.stderr.write(str(trained)+"\n") + f.write("\t".join([user, str(len(saved['saved_tags'])), str(trained)])+"/n") + k = trained.keys() + k.remove(u'status') + ntrain = trained[k[0]][0][u'training_set_size'] + + print ",".join([user,str(count),str(len(saved['saved_tags'])),str(ntrain)]) + + +if __name__ == "__main__": + train(sys.argv[1],sys.argv[2],sys.argv[3:]) + + diff --git a/data/faces.py b/data/faces.py index 8e60307..491dc5c 100755 --- a/data/faces.py +++ b/data/faces.py @@ -1,12 +1,13 @@ #!/usr/bin/python +import os import cv import sys import math sk_file = open(sys.argv[1]) pic_dir = sys.argv[2] + '/' -out_dir = '~/out/' +out_dir = '/home/jbw/out/'+sys.argv[1].split('/')[-2] + '/' pframe = 0 limbs = {} j1 = 'Head' @@ -18,27 +19,23 @@ for line in sk_file: if frame != 'Frame': run = int(run) frame = int(frame) - if frame != pframe: - if j1 in limbs: - print 'Monkey1' - if j2 in limbs: - print 'Monkey2' - print 'end' + fout = out_dir+str(frame)+'.jpg' + if frame != pframe: #and not os.path.exists(fout): if j1 in limbs and j2 in limbs: - print 'Monkey3' dx = limbs[j1][0] - limbs[j2][0] dy = limbs[j1][1] - limbs[j2][1] img = cv.LoadImage(pic_dir+str(pframe)+".jpg") r = math.sqrt(dx*dx+dy*dy) - x1 = max(int(limbs[j1][0] - r),0) - y1 = max(int(limbs[j1][1] - r),0) - x2 = min(int(x1 + 2*r), img.width) - y2 = min(int(y1 + 3*r), img.height) - sub = cv.GetSubRect(img,(x1,y1,x2-x1,y2-y1)) + x1 = max(int(limbs[j1][0] - 1.5*r),0) + y1 = max(int(limbs[j1][1] - 1.5*r),0) + x2 = min(int(x1 + 3*r), img.width-1) + y2 = min(int(y1 + 3*r), img.height-1) + sub = cv.GetSubRect(img,(x1,y1,max(x2-x1,1),max(y2-y1,1))) #cv.Rectangle(img,(x1,y1),(x2,y2),(0,255,0),3,8,0) - cv.SaveImage(out_dir+str(run)+"-"+str(frame)+'.jpg',sub) - cv.ShowImage('video', img) + print fout + cv.SaveImage(fout,sub) + #cv.ShowImage('video', img) cv.WaitKey(5) pframe = frame limbs = {} diff --git a/data/multipart.py b/data/multipart.py new file mode 100755 index 0000000..836b968 --- /dev/null +++ b/data/multipart.py @@ -0,0 +1,149 @@ +''' +Classes for using multipart form data from Python, which does not (at the +time of writing) support this directly. + +To use this, make an instance of Multipart and add parts to it via the factory +methods field and file. When you are done, get the content via the get method. + +@author: Stacy Prowell (http://stacyprowell.com) +''' + +import mimetypes + + +class Part(object): + ''' + Class holding a single part of the form. You should never need to use + this class directly; instead, use the factory methods in Multipart: + field and file. + ''' + + # The boundary to use. This is shamelessly taken from the standard. + BOUNDARY = '----------AaB03x' + CRLF = '\r\n' + # Common headers. + CONTENT_TYPE = 'Content-Type' + CONTENT_DISPOSITION = 'Content-Disposition' + # The default content type for parts. + DEFAULT_CONTENT_TYPE = 'application/octet-stream' + + def __init__(self, name, filename, body, headers): + ''' + Make a new part. The part will have the given headers added initially. + + @param name: The part name. + @type name: str + @param filename: If this is a file, the name of the file. Otherwise + None. + @type filename: str + @param body: The body of the part. + @type body: str + @param headers: Additional headers, or overrides, for this part. + You can override Content-Type here. + @type headers: dict + ''' + self._headers = headers.copy() + self._name = name + self._filename = filename + self._body = body + # We respect any content type passed in, but otherwise set it here. + # We set the content disposition now, overwriting any prior value. + if self._filename == None: + self._headers[Part.CONTENT_DISPOSITION] = \ + ('form-data; name="%s"' % self._name) + self._headers.setdefault(Part.CONTENT_TYPE, + Part.DEFAULT_CONTENT_TYPE) + else: + self._headers[Part.CONTENT_DISPOSITION] = \ + ('form-data; name="%s"; filename="%s"' % + (self._name, self._filename)) + self._headers.setdefault(Part.CONTENT_TYPE, + mimetypes.guess_type(filename)[0] + or Part.DEFAULT_CONTENT_TYPE) + return + + def get(self): + ''' + Convert the part into a list of lines for output. This includes + the boundary lines, part header lines, and the part itself. A + blank line is included between the header and the body. + + @return: Lines of this part. + @rtype: list + ''' + lines = [] + lines.append('--' + Part.BOUNDARY) + for (key, val) in self._headers.items(): + lines.append(str('%s: %s' % (key, val))) + lines.append('') + lines.append(self._body) + return lines + + +class Multipart(object): + ''' + Encapsulate multipart form data. To use this, make an instance and then + add parts to it via the two methods (field and file). When done, you can + get the result via the get method. + + See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 for + details on multipart/form-data. + + Watch http://bugs.python.org/issue3244 to see if this is fixed in the + Python libraries. + + @return: content type, body + @rtype: tuple + ''' + + def __init__(self): + self.parts = [] + return + + def field(self, name, value, headers={}): + ''' + Create and append a field part. This kind of part has a field name + and value. + + @param name: The field name. + @type name: str + @param value: The field value. + @type value: str + @param headers: Headers to set in addition to disposition. + @type headers: dict + ''' + self.parts.append(Part(name, None, value, headers)) + return + + def file(self, name, filename, value, headers={}): + ''' + Create and append a file part. THis kind of part has a field name, + a filename, and a value. + + @param name: The field name. + @type name: str + @param value: The field value. + @type value: str + @param headers: Headers to set in addition to disposition. + @type headers: dict + ''' + self.parts.append(Part(name, filename, value, headers)) + return + + def get(self): + ''' + Get the multipart form data. This returns the content type, which + specifies the boundary marker, and also returns the body containing + all parts and bondary markers. + + @return: content type, body + @rtype: tuple + ''' + all = [] + for part in self.parts: + all += part.get() + all.append('--' + Part.BOUNDARY + '--') + all.append('') + # We have to return the content type, since it specifies the boundary. + content_type = 'multipart/form-data; boundary=%s' % Part.BOUNDARY + return content_type, Part.CRLF.join(all) -- cgit v1.2.3-70-g09d2