Skip to content

Commit 84a5c6f

Browse files
committed
registration and bug fixes
1 parent 490fafc commit 84a5c6f

26 files changed

Lines changed: 871 additions & 99 deletions

dliblib/dliblib.ini

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
[train_shape_predictor]
2-
images_folder=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized
3-
training_xml_file=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/vgg_keypoints.xml
4-
predictor_out=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/predictor.dat
2+
images_folder=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized
3+
training_xml_file=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/vgg_landmarks.xml
4+
predictor_out=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/predictor.dat
5+
6+
7+
[predict]
8+
images_folder=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/charter/fujifilm/rotated
9+
vgg_file=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/charter/fujifilm/rotated/vgg_testdlib.json
10+
predictor_out=C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/predictor.dat

dliblib/dliblib.pyproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<InterpreterId />
1616
<InterpreterVersion />
1717
<IsWindowsApplication>False</IsWindowsApplication>
18-
<CommandLineArguments>"C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/vgg_landmarks_633bad.json"</CommandLineArguments>
18+
<CommandLineArguments>"C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_grow/resized/vgg_landmarks.json"</CommandLineArguments>
1919
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging>
2020
<Environment>vgg_pts_to_xml.py "C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/vgg_keypoints.json" "C:/Users/Graham Monkman/OneDrive/Documents/PHD/images/bass/fiducial/train/candidate/roi_whole/resized/vgg_keypoints.xml"
2121

@@ -29,6 +29,9 @@
2929
</PropertyGroup>
3030
<ItemGroup>
3131
<Compile Include="ini.py" />
32+
<Compile Include="scripts\predict.py">
33+
<SubType>Code</SubType>
34+
</Compile>
3235
<Compile Include="scripts\train_shape_predictor.py">
3336
<SubType>Code</SubType>
3437
</Compile>

dliblib/scripts/predict.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# pylint: disable=C0103, too-few-public-methods, locally-disabled, no-self-use, unused-argument
2+
'''
3+
Run shape predictor example.
4+
5+
Reads resources from dliblib.ini
6+
'''
7+
import os
8+
import os.path as path
9+
import sys
10+
import glob
11+
import dlib
12+
13+
import numpy as np
14+
15+
import dliblib.ini as ini
16+
from funclib import iolib
17+
from opencvlib.info import ImageInfo
18+
from opencvlib.view import show
19+
from opencvlib.common import draw_points
20+
from opencvlib.display_utils import KeyBoardInput as Key
21+
from opencvlib import getimg
22+
from opencvlib.imgpipes import vgg
23+
24+
images_folder = path.normpath(ini.Cfg.tryread('predict', 'images_folder'))
25+
assert iolib.folder_exists(images_folder), 'Images folder %s not found or not specified in ini file.' % images_folder
26+
27+
predictor_dat = path.normpath(ini.Cfg.tryread('predict', 'predictor_out'))
28+
predictor = dlib.shape_predictor(predictor_dat)
29+
30+
vgg_file = path.normpath(ini.Cfg.tryread('predict', 'vgg_file'))
31+
32+
33+
print("Press 'q' to quit")
34+
35+
vgg.load_json(vgg_file)
36+
for Img in vgg.imagesGenerator():
37+
assert isinstance(Img, vgg.Image)
38+
i = getimg(path.normpath(Img.filepath))
39+
for roi in Img.roi_generator(shape_type='rect'):
40+
assert isinstance(roi, vgg.Region)
41+
d = dlib.rectangle(min(roi.all_points_x), max(roi.all_points_x), min(roi.all_points_y), max(roi.all_points_y))
42+
i = draw_points(roi.all_points, i, join=True)
43+
shape = predictor(i, d)
44+
45+
pts = [(pt.x, pt.y) for pt in shape.parts()]
46+
i = draw_points(pts, i)
47+
if Key.check_pressed_key('q', show(i)):
48+
quit()
49+
50+
51+

dliblib/scripts/train_shape_predictor.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Set the image folders and the path to the XML
66
file which defines the points in dliblib.ini
77
in the root of this module.
8+
9+
Create the training XML from running dliblib.vgg2xml first.
810
'''
911
import os
1012
import sys
@@ -32,42 +34,44 @@
3234
options.be_verbose = True
3335

3436
dlib.train_shape_predictor(training_xml_file, predictor_out, options)
35-
print("\nTraining accuracy: {}".format(
36-
dlib.test_shape_predictor(training_xml_file, predictor_out)))
37-
38-
#testing_xml_path = os.path.join(images_folder, "testing_with_face_landmarks.xml")
39-
#print("Testing accuracy: {}".format(
40-
# dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
41-
42-
# Now let's use it as you would in a normal application. First we will load it
43-
# from disk. We also need to load a face detector to provide the initial
44-
# estimate of the facial location.
45-
predictor = dlib.shape_predictor("predictor.dat")
46-
detector = dlib.get_frontal_face_detector()
47-
48-
49-
print("Showing detections and predictions on the images in the faces folder...")
50-
win = dlib.image_window()
51-
for f in glob.glob(os.path.join(images_folder, "*.jpg")):
52-
print("Processing file: {}".format(f))
53-
img = io.imread(f)
54-
55-
win.clear_overlay()
56-
win.set_image(img)
57-
58-
dets = detector(img, 1)
59-
print("Number of faces detected: {}".format(len(dets)))
60-
for k, d in enumerate(dets):
61-
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
62-
k, d.left(), d.top(), d.right(), d.bottom()))
63-
64-
# Get the landmarks/parts for the face in box d.
65-
shape = predictor(img, d)
66-
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
67-
shape.part(1)))
68-
# Draw the face landmarks on the screen.
69-
win.add_overlay(shape)
70-
71-
win.add_overlay(dets)
72-
dlib.hit_enter_to_continue()
37+
print("\nTraining accuracy: {}".format(dlib.test_shape_predictor(training_xml_file, predictor_out)))
38+
39+
40+
41+
if False:
42+
#testing_xml_path = os.path.join(images_folder, "testing_with_face_landmarks.xml")
43+
#print("Testing accuracy: {}".format(
44+
# dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
45+
46+
# Now let's use it as you would in a normal application. First we will load it
47+
# from disk. We also need to load a face detector to provide the initial
48+
# estimate of the facial location.
49+
predictor = dlib.shape_predictor("predictor.dat")
50+
detector = dlib.get_frontal_face_detector() #find the faces
51+
52+
if False:
53+
print("Showing detections and predictions on the images in the faces folder...")
54+
win = dlib.image_window()
55+
for f in glob.glob(os.path.join(images_folder, "*.jpg")):
56+
print("Processing file: {}".format(f))
57+
img = io.imread(f)
58+
59+
win.clear_overlay()
60+
win.set_image(img)
61+
62+
dets = detector(img, 1)
63+
print("Number of faces detected: {}".format(len(dets)))
64+
for k, d in enumerate(dets):
65+
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
66+
k, d.left(), d.top(), d.right(), d.bottom()))
67+
68+
# Get the landmarks/parts for the face in box d.
69+
shape = predictor(img, d)
70+
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
71+
shape.part(1)))
72+
# Draw the face landmarks on the screen.
73+
win.add_overlay(shape)
74+
75+
win.add_overlay(dets)
76+
dlib.hit_enter_to_continue()
7377

dliblib/scripts/validate_points2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ def main():
6161
lbl = int(vggReg.region_json_key) + 1 if vggReg.region_attr.get('pts', '') == '' else int(vggReg.region_attr['pts'])
6262
Pts[lbl-1] = [vggReg.x, vggReg.y]
6363

64-
train_pts[j, ...] = np.array(Pts)
64+
train_pts[j, ...] = np.array(Pts) #this contains all the points
6565
train_distances[j, :, :] = distances(Pts, Pts).squeeze()
6666
train_angles[j, ...] = angles_between(Pts, Pts).squeeze()
6767
j += 1
6868

6969
mean_distances = np.nanmean(train_distances, axis=0) #19x19 array of mean distances between points
7070
mean_angles = np.nanmean(train_angles, axis=0)
71-
71+
mean_points = np.nanmean(train_pts, axis=0)
7272

7373
#region Load all data
7474
x = sum([1 for x in _vgg.imagesGenerator(skip_imghdr_check=True)])

dliblib/vgg2xml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# pylint: disable=C0103, too-few-public-methods, locally-disabled, unused-import
2-
'''Work with vgg and the dblib/W-300 point format files'''
2+
'''Work with vgg and the dblib/W-300 point format files.
3+
4+
Currently just works with images, rather than roi with
5+
points defined inside.
6+
7+
The W300 XML format was designed to accept an image with
8+
face regions defined by top, left, width and height coords
9+
and points are defined in a CVXY coord system with respect
10+
to the image (and not the bounding box)'''
311
import xml.etree.ElementTree as _et
412
from xml.dom import minidom as _minidom
513
from os import path as _path

funclib/arraylib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def hstackt(arrays):
5151
return out
5252

5353

54+
5455
def check_array(a, b):
5556
'''(ndarray,ndarray)
5657
perform array checks raising error if problem
@@ -358,8 +359,6 @@ def distances(origs, dests):
358359
Create 2d array of distance between n-dimensional points
359360
360361
origs is broadcasted to dests
361-
362-
363362
'''
364363
nd_o = makenp(origs)
365364
nd_d = makenp(dests)

futerize_userdict.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
REM c:\python352\Scripts\futurize -w -1 "C:/Python354x64/Lib/site-packages/UserDict.py"
2+
c:\python352\Scripts\futurize -w -1 "C:/Python354x64/Lib/site-packages/_abcoll.py"
3+
pause

0 commit comments

Comments
 (0)