Commit 0f45059bf7eb1ff628616b9bc1a332522fb4cff0
1 parent
34a914c4
Add some landmarks HTML visualizations
Showing
1 changed file
with
35 additions
and
2 deletions
scripts/brpy/html_viz.py
| @@ -8,12 +8,46 @@ and host them as long as the relative paths remain the same on ya serva. | @@ -8,12 +8,46 @@ and host them as long as the relative paths remain the same on ya serva. | ||
| 8 | 8 | ||
| 9 | from PIL import Image | 9 | from PIL import Image |
| 10 | 10 | ||
| 11 | +LMSIZE = 8 | ||
| 12 | + | ||
| 13 | +def landmarks_on_crop(landmarks, x, y, width, height, imname, maxheight=None, color='green'): | ||
| 14 | + ''' | ||
| 15 | + Generates an HTML string that shows landmarks within a given cropped image. | ||
| 16 | + When two landmarked crops are put next to each other, they will be inline. To make each crop its own line, wrap it in a div. | ||
| 17 | + ''' | ||
| 18 | + html = _bbcrop(x, y, width, height, imname, maxheight) | ||
| 19 | + if not maxheight: | ||
| 20 | + maxheight = height | ||
| 21 | + ratio = float(maxheight) / height | ||
| 22 | + print(ratio) | ||
| 23 | + if len(landmarks) > 0 and len(landmarks[0]) != 3: | ||
| 24 | + landmarks = [ lm + (color,) for lm in landmarks ] | ||
| 25 | + for lmx,lmy,col in landmarks: | ||
| 26 | + html += landmark((lmx-x)*ratio - (LMSIZE/2), (lmy-y)*ratio - (LMSIZE/2), col) | ||
| 27 | + html += '</div>' | ||
| 28 | + return html | ||
| 29 | + | ||
| 30 | +def landmark_img(x, y, img, color='green'): | ||
| 31 | + html = '<div style="position:relative;">' | ||
| 32 | + html += '<img src="%s" />' % img | ||
| 33 | + html += landmark(x,y,color) | ||
| 34 | + html += '</div>' | ||
| 35 | + return html | ||
| 36 | + | ||
| 37 | +def landmark(x, y, color='green'): | ||
| 38 | + return '<div style="position:absolute; top:{0}px; left:{1}px; color:{2}; background-color:{2}; width:{3}px; height:{3}px;"></div>'.format(y,x,color,LMSIZE) | ||
| 39 | + | ||
| 11 | def crop_to_bb(x, y, width, height, imname, maxheight=None): | 40 | def crop_to_bb(x, y, width, height, imname, maxheight=None): |
| 12 | ''' | 41 | ''' |
| 13 | Generates an HTML string that crops to a given bounding box and resizes to maxheight pixels. | 42 | Generates an HTML string that crops to a given bounding box and resizes to maxheight pixels. |
| 14 | A maxheight of None will keep the original size (default). | 43 | A maxheight of None will keep the original size (default). |
| 15 | When two crops are put next to each other, they will be inline. To make each crop its own line, wrap it in a div. | 44 | When two crops are put next to each other, they will be inline. To make each crop its own line, wrap it in a div. |
| 16 | ''' | 45 | ''' |
| 46 | + html = _bbcrop(x, y, width, height, imname, maxheight) | ||
| 47 | + html += '</div>' | ||
| 48 | + return html | ||
| 49 | + | ||
| 50 | +def _bbcrop(x, y, width, height, imname, maxheight): | ||
| 17 | img = Image.open(imname) | 51 | img = Image.open(imname) |
| 18 | imwidth, imheight = img.size | 52 | imwidth, imheight = img.size |
| 19 | if not maxheight: | 53 | if not maxheight: |
| @@ -23,9 +57,8 @@ def crop_to_bb(x, y, width, height, imname, maxheight=None): | @@ -23,9 +57,8 @@ def crop_to_bb(x, y, width, height, imname, maxheight=None): | ||
| 23 | # image is cropped with div width/height + overflow:hidden, | 57 | # image is cropped with div width/height + overflow:hidden, |
| 24 | # resized with img height, | 58 | # resized with img height, |
| 25 | # and positioned with img margin | 59 | # and positioned with img margin |
| 26 | - html = '<div style="overflow:hidden; display:inline-block; width:%ipx; height:%ipx;">' % (width*ratio, maxheight) | 60 | + html = '<div style="overflow:hidden; display:inline-block; position:relative; width:%ipx; height:%ipx;">' % (width*ratio, maxheight) |
| 27 | html += '<img src="%s" style="height:%ipx; margin:-%ipx 0 0 -%ipx;"/>' % (imname, imheight*ratio, y*ratio, x*ratio) | 61 | html += '<img src="%s" style="height:%ipx; margin:-%ipx 0 0 -%ipx;"/>' % (imname, imheight*ratio, y*ratio, x*ratio) |
| 28 | - html += '</div>' | ||
| 29 | return html | 62 | return html |
| 30 | 63 | ||
| 31 | def bbs_for_image(imname, bbs, maxheight=None, colors=None): | 64 | def bbs_for_image(imname, bbs, maxheight=None, colors=None): |