Commit 6ad9e3fac54a8cac791d2a154fb0766a2b16c010
1 parent
86572054
Show add comparison to python example
Showing
1 changed file
with
35 additions
and
12 deletions
docs/docs/api_docs/python_api.md
| 1 | 1 | # Python API |
| 2 | 2 | |
| 3 | -The Python API is a light wrapper of the C API. It creates an object that has [all the C functions](c_api/functions.md): | |
| 3 | +The Python API is a light wrapper of the C API. It creates an object that has [all the C functions](c_api/functions.md). Here is a fictional example that, given a `MagicalImageURLGenerator` and a `CatFaceRecognitionModel`, will find cat faces most similar to the cats in a query image. | |
| 4 | 4 | |
| 5 | 5 | from brpy import init_brpy |
| 6 | + import requests # or whatever http request lib you prefer | |
| 7 | + import MagicalImageURLGenerator # made up | |
| 6 | 8 | |
| 7 | 9 | # br_loc is /usr/local/lib by default, |
| 8 | 10 | # you may change this by passing a different path to the shared objects |
| 9 | 11 | br = init_brpy(br_loc='/path/to/libopenbr') |
| 10 | 12 | br.br_initialize_default() |
| 11 | - | |
| 12 | - img = open('catpic.jpg','rb').read() | |
| 13 | - | |
| 14 | - br.br_set_property('algorithm','MyCatFaceDetectionModel') | |
| 13 | + br.br_set_property('algorithm','CatFaceRecognitionModel') # also made up | |
| 15 | 14 | br.br_set_property('enrollAll','true') |
| 16 | 15 | |
| 17 | - tmpl = br.br_load_img(img, len(img)) | |
| 18 | - catfaces = br.br_enroll_template(tmpl) | |
| 19 | - | |
| 20 | - print('This pic has %i cats in it!' % br.br_num_templates(catfaces)) | |
| 21 | - | |
| 22 | - br.br_free_template(tmpl) | |
| 23 | - br.br_free_template_list(catfaces) | |
| 16 | + mycatsimg = open('mycats.jpg', 'rb').read() # cat picture not provided =^..^= | |
| 17 | + mycatstmpl = br.br_load_img(mycatsimg, len(mycatsimg)) | |
| 18 | + query = br.br_enroll_template(mycatstmpl) | |
| 19 | + nqueries = br.br_num_templates(query) | |
| 20 | + | |
| 21 | + scores = [] | |
| 22 | + for imurl in MagicalImageURLGenerator(): | |
| 23 | + # load and enroll image from URL | |
| 24 | + img = requests.get(imurl).content | |
| 25 | + tmpl = br.br_load_img(img, len(img)) | |
| 26 | + targets = br.br_enroll_template(tmpl) | |
| 27 | + ntargets = br.br_num_templates(targets) | |
| 28 | + | |
| 29 | + # compare and collect scores | |
| 30 | + scoresmat = br.br_compare_template_lists(targets, query) | |
| 31 | + for r in range(ntargets): | |
| 32 | + for c in range(nqueries): | |
| 33 | + scores.append((imurl, br.br_get_matrix_output_at(scoresmat, r, c))) | |
| 34 | + | |
| 35 | + # clean up - no memory leaks | |
| 36 | + br.br_free_template(tmpl) | |
| 37 | + br.br_free_template_list(targets) | |
| 38 | + | |
| 39 | + # print top 10 match URLs | |
| 40 | + scores.sort(key=lambda s: s[1]) | |
| 41 | + for s in scores[:10]: | |
| 42 | + print(s[0]) | |
| 43 | + | |
| 44 | + # clean up - no memory leaks | |
| 45 | + br.br_free_template(mycatstmpl) | |
| 46 | + br.br_free_template_list(query) | |
| 24 | 47 | br.br_finalize() |
| 25 | 48 | |
| 26 | 49 | To enable the module, add `-DBR_INSTALL_BRPY=ON` to your cmake command (or use the ccmake GUI - highly recommended). | ... | ... |