-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook.tex
984 lines (798 loc) · 81 KB
/
notebook.tex
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
% Default to the notebook output style
% Inherit from the specified cell style.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
% Nicer default font (+ math font) than Computer Modern for most use cases
\usepackage{mathpazo}
% Basic figure setup, for now with no caption control since it's done
% automatically by Pandoc (which extracts ![](path) syntax from Markdown).
\usepackage{graphicx}
% We will generate all images so they have a width \maxwidth. This means
% that they will get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
% Set max figure width to be 80% of text width, for now hardcoded.
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=.8\maxwidth]{#1}}
% Ensure that by default, figures have no caption (until we provide a
% proper Figure object with a Caption API and a way to capture that
% in the conversion process - todo).
\usepackage{caption}
\DeclareCaptionLabelFormat{nolabel}{}
\captionsetup{labelformat=nolabel}
\usepackage{adjustbox} % Used to constrain images to a maximum size
\usepackage{xcolor} % Allow colors to be defined
\usepackage{enumerate} % Needed for markdown enumerations to work
\usepackage{geometry} % Used to adjust the document margins
\usepackage{amsmath} % Equations
\usepackage{amssymb} % Equations
\usepackage{textcomp} % defines textquotesingle
% Hack from http://tex.stackexchange.com/a/47451/13684:
\AtBeginDocument{%
\def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
}
\usepackage{upquote} % Upright quotes for verbatim code
\usepackage{eurosym} % defines \euro
\usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
\usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document
\usepackage{fancyvrb} % verbatim replacement that allows latex
\usepackage{grffile} % extends the file name processing of package graphics
% to support a larger range
% The hyperref package gives us a pdf with properly built
% internal navigation ('pdf bookmarks' for the table of contents,
% internal cross-reference links, web links for URLs, etc.)
\usepackage{hyperref}
\usepackage{longtable} % longtable support required by pandoc >1.10
\usepackage{booktabs} % table support for pandoc > 1.12.2
\usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
\usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
% normalem makes italics be italics, not underlines
% Colors for the hyperref package
\definecolor{urlcolor}{rgb}{0,.145,.698}
\definecolor{linkcolor}{rgb}{.71,0.21,0.01}
\definecolor{citecolor}{rgb}{.12,.54,.11}
% ANSI colors
\definecolor{ansi-black}{HTML}{3E424D}
\definecolor{ansi-black-intense}{HTML}{282C36}
\definecolor{ansi-red}{HTML}{E75C58}
\definecolor{ansi-red-intense}{HTML}{B22B31}
\definecolor{ansi-green}{HTML}{00A250}
\definecolor{ansi-green-intense}{HTML}{007427}
\definecolor{ansi-yellow}{HTML}{DDB62B}
\definecolor{ansi-yellow-intense}{HTML}{B27D12}
\definecolor{ansi-blue}{HTML}{208FFB}
\definecolor{ansi-blue-intense}{HTML}{0065CA}
\definecolor{ansi-magenta}{HTML}{D160C4}
\definecolor{ansi-magenta-intense}{HTML}{A03196}
\definecolor{ansi-cyan}{HTML}{60C6C8}
\definecolor{ansi-cyan-intense}{HTML}{258F8F}
\definecolor{ansi-white}{HTML}{C5C1B4}
\definecolor{ansi-white-intense}{HTML}{A1A6B2}
% commands and environments needed by pandoc snippets
% extracted from the output of `pandoc -s`
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
% Additional commands for more recent versions of Pandoc
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
\newcommand{\ImportTok}[1]{{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
\newcommand{\BuiltInTok}[1]{{#1}}
\newcommand{\ExtensionTok}[1]{{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
% Define a nice break command that doesn't care if a line doesn't already
% exist.
\def\br{\hspace*{\fill} \\* }
% Math Jax compatability definitions
\def\gt{>}
\def\lt{<}
% Document parameters
\title{Untitled}
% Pygments definitions
\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
\let\PY@ul=\relax \let\PY@tc=\relax%
\let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
\PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
\PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit}
\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf}
\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZhy{\char`\-}
\def\PYZsq{\char`\'}
\def\PYZdq{\char`\"}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother
% Exact colors from NB
\definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
\definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}
% Prevent overflowing lines due to hard-to-break entities
\sloppy
% Setup hyperref package
\hypersetup{
breaklinks=true, % so long urls are correctly broken across lines
colorlinks=true,
urlcolor=urlcolor,
linkcolor=linkcolor,
citecolor=citecolor,
}
% Slightly bigger margins than the latex defaults
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\begin{document}
\maketitle
\textbf{Advanced Lane Finding Project}
The goals / steps of this project are the following:
\begin{itemize}
\tightlist
\item
Compute the camera calibration matrix and distortion coefficients
given a set of chessboard images.
\item
Apply a distortion correction to raw images.
\item
Use color transforms, gradients, etc., to create a thresholded binary
image.
\item
Apply a perspective transform to rectify binary image (``birds-eye
view'').
\item
Detect lane pixels and fit to find the lane boundary.
\item
Determine the curvature of the lane and vehicle position with respect
to center.
\item
Warp the detected lane boundaries back onto the original image.
\item
Output visual display of the lane boundaries and numerical estimation
of lane curvature and vehicle position.
\end{itemize}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}1}]:} \PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np}
\PY{k+kn}{import} \PY{n+nn}{matplotlib}\PY{n+nn}{.}\PY{n+nn}{pyplot} \PY{k}{as} \PY{n+nn}{plt}
\PY{k+kn}{import} \PY{n+nn}{cv2}
\PY{k+kn}{import} \PY{n+nn}{glob}
\end{Verbatim}
\hypertarget{camera-calibration}{%
\subsubsection{Camera Calibration}\label{camera-calibration}}
I start by preparing ``object points'', which will be the (x, y, z)
coordinates of the chessboard corners in the world. Here I am assuming
the chessboard is fixed on the (x, y) plane at z=0, such that the object
points are the same for each calibration image. Thus, \texttt{obj} is
just a replicated array of coordinates, and \texttt{object\_points} will
be appended with a copy of it every time I successfully detect all
chessboard corners in a test image. \texttt{image\_points} will be
appended with the (x, y) pixel position of each of the corners in the
image plane with each successful chessboard detection.
I then used the output \texttt{object\_points} and
\texttt{image\_points} to compute the camera calibration and distortion
coefficients using the \texttt{cv2.calibrateCamera()} function.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}2}]:} \PY{k}{def} \PY{n+nf}{calibrate\PYZus{}camera}\PY{p}{(}\PY{n}{path}\PY{p}{)}\PY{p}{:}
\PY{n}{object\PYZus{}points}\PY{o}{=}\PY{p}{[}\PY{p}{]}
\PY{n}{image\PYZus{}points}\PY{o}{=}\PY{p}{[}\PY{p}{]}
\PY{n}{images}\PY{o}{=}\PY{n}{glob}\PY{o}{.}\PY{n}{glob}\PY{p}{(}\PY{n}{path}\PY{p}{)}
\PY{n}{img\PYZus{}size}\PY{o}{=}\PY{p}{(}\PY{p}{)}
\PY{n}{obj}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{9}\PY{o}{*}\PY{l+m+mi}{6}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,}\PY{n}{np}\PY{o}{.}\PY{n}{float32}\PY{p}{)}
\PY{n}{obj}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{l+m+mi}{2}\PY{p}{]}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{mgrid}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{:}\PY{l+m+mi}{9}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{:}\PY{l+m+mi}{6}\PY{p}{]}\PY{o}{.}\PY{n}{T}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)}
\PY{k}{for} \PY{n}{idx}\PY{p}{,}\PY{n}{image} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{images}\PY{p}{)}\PY{p}{:}
\PY{n}{img}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{imread}\PY{p}{(}\PY{n}{image}\PY{p}{)}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{img}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2GRAY}\PY{p}{)}
\PY{n}{ret}\PY{p}{,}\PY{n}{corners}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{findChessboardCorners}\PY{p}{(}\PY{n}{gray}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{9}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{,}\PY{k+kc}{None}\PY{p}{)}
\PY{k}{if} \PY{n}{ret}\PY{p}{:}
\PY{n}{image\PYZus{}points}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{corners}\PY{p}{)}
\PY{n}{object\PYZus{}points}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{obj}\PY{p}{)}
\PY{n}{img\PYZus{}size}\PY{o}{=}\PY{p}{(}\PY{n}{img}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,}\PY{n}{img}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{ret}\PY{p}{,}\PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{p}{,}\PY{n}{rvecs}\PY{p}{,}\PY{n}{tvecs}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{calibrateCamera}\PY{p}{(}\PY{n}{object\PYZus{}points}\PY{p}{,}\PY{n}{image\PYZus{}points}\PY{p}{,}\PY{n}{img\PYZus{}size}\PY{p}{,}\PY{k+kc}{None}\PY{p}{,}\PY{k+kc}{None}\PY{p}{)}
\PY{k}{return} \PY{n}{mtx}\PY{p}{,}\PY{n}{dist}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{o}{=}\PY{n}{calibrate\PYZus{}camera}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./camera\PYZus{}cal/calibration*.jpg}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}
In the previous step I calculated \emph{Camera Matrix} and
\emph{Distortion Coefficients} using \texttt{cv2.calibrateCamera} now I
can use these values to undistort any image taken from the same camera
using \texttt{cv2.undistort}. The result after distorting image looks
like this:
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}4}]:} \PY{n}{test\PYZus{}image}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{imread}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./camera\PYZus{}cal/calibration2.jpg}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{undistort}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{undistort}\PY{p}{(}\PY{n}{test\PYZus{}image}\PY{p}{,}\PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{p}{,}\PY{k+kc}{None}\PY{p}{,}\PY{n}{mtx}\PY{p}{)}
\PY{n}{f}\PY{p}{,} \PY{p}{(}\PY{n}{ax1}\PY{p}{,} \PY{n}{ax2}\PY{p}{)} \PY{o}{=} \PY{n}{plt}\PY{o}{.}\PY{n}{subplots}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{n}{figsize}\PY{o}{=}\PY{p}{(}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{test\PYZus{}image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2RGB}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Original Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{undistort}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2RGB}\PY{p}{)}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Undistorted Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}4}]:} <matplotlib.text.Text at 0x17646c80be0>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_6_1.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{perspective-transform}{%
\subsubsection{Perspective Transform}\label{perspective-transform}}
I started with choosing four reference points in the image to use them
as \texttt{src\_points} and four points for \texttt{dest\_points}. The
source points reflects on destination points after the perspective
transform. The first step is to calculate the transformation matrix
using \texttt{cv2.getPerspectiveTransform()} this function takes source
and destination points as input.
\begin{longtable}[]{@{}cc@{}}
\toprule
Source & Destination\tabularnewline
\midrule
\endhead
200, 720 & 300, 720\tabularnewline
600, 447 & 300, 0\tabularnewline
679, 447 & 900, 0\tabularnewline
1100,720 & 900, 720\tabularnewline
\bottomrule
\end{longtable}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{c+c1}{\PYZsh{}function to calculate transformation matrix for perspective transform.}
\PY{k}{def} \PY{n+nf}{get\PYZus{}transformation\PYZus{}matrix}\PY{p}{(}\PY{n}{src}\PY{p}{,}\PY{n}{dest}\PY{p}{)}\PY{p}{:}
\PY{n}{transformation\PYZus{}matrix}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{getPerspectiveTransform}\PY{p}{(}\PY{n}{src}\PY{p}{,}\PY{n}{dest}\PY{p}{)}
\PY{k}{return} \PY{n}{transformation\PYZus{}matrix}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}6}]:} \PY{c+c1}{\PYZsh{}warp the image}
\PY{k}{def} \PY{n+nf}{perspective\PYZus{}transform}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{transformation\PYZus{}matrix}\PY{p}{)}\PY{p}{:}
\PY{n}{img\PYZus{}size}\PY{o}{=}\PY{p}{(}\PY{n}{image}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,}\PY{n}{image}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{transformed\PYZus{}image}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{warpPerspective}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{transformation\PYZus{}matrix}\PY{p}{,}\PY{n}{img\PYZus{}size}\PY{p}{,}\PY{n}{flags}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{INTER\PYZus{}LINEAR}\PY{p}{)}
\PY{k}{return} \PY{n}{transformed\PYZus{}image}
\end{Verbatim}
\hypertarget{perform-perspective-transform}{%
\paragraph{Perform Perspective
Transform}\label{perform-perspective-transform}}
Once we have the transformation matrix we can use
\texttt{cv2.warpPerspective} to perform perspective transform of an
image. The result after the transform looks like this.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}7}]:} \PY{c+c1}{\PYZsh{}undistort Image}
\PY{n}{transformation\PYZus{}test}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{imread}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./frames/frame2.jpg}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{undistort}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{undistort}\PY{p}{(}\PY{n}{transformation\PYZus{}test}\PY{p}{,}\PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{p}{,}\PY{k+kc}{None}\PY{p}{,}\PY{n}{mtx}\PY{p}{)}
\PY{c+c1}{\PYZsh{}src\PYZus{}points=np.float32([[790,490],[1090,670],[250,670],[515,490]])}
\PY{c+c1}{\PYZsh{}dest\PYZus{}points=np.float32([[1100,0],[1100,720],[200,720],[200,0]])}
\PY{c+c1}{\PYZsh{}src\PYZus{}points=np.float32([[800,490],[1100,650],[210,650],[500,490]])}
\PY{c+c1}{\PYZsh{}dest\PYZus{}points=np.float32([[1100,0],[1100,720],[200,720],[200,0]])}
\PY{c+c1}{\PYZsh{}perform perspective transform}
\PY{n}{src\PYZus{}points} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{float32}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{200}\PY{p}{,}\PY{l+m+mi}{720}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{600}\PY{p}{,} \PY{l+m+mi}{447}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{679}\PY{p}{,}\PY{l+m+mi}{447}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{1100}\PY{p}{,}\PY{l+m+mi}{720}\PY{p}{]}\PY{p}{]}\PY{p}{)}
\PY{n}{dest\PYZus{}points} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{float32}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{300}\PY{p}{,} \PY{l+m+mi}{720}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{300}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{900}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{900}\PY{p}{,} \PY{l+m+mi}{720}\PY{p}{]}\PY{p}{]}\PY{p}{)}
\PY{n}{transformation\PYZus{}matrix}\PY{o}{=}\PY{n}{get\PYZus{}transformation\PYZus{}matrix}\PY{p}{(}\PY{n}{src\PYZus{}points}\PY{p}{,}\PY{n}{dest\PYZus{}points}\PY{p}{)}
\PY{n}{transformed\PYZus{}image}\PY{o}{=}\PY{n}{perspective\PYZus{}transform}\PY{p}{(}\PY{n}{undistort}\PY{p}{,}\PY{n}{transformation\PYZus{}matrix}\PY{p}{)}
\PY{n}{f}\PY{p}{,} \PY{p}{(}\PY{n}{ax1}\PY{p}{,} \PY{n}{ax2}\PY{p}{)} \PY{o}{=} \PY{n}{plt}\PY{o}{.}\PY{n}{subplots}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{n}{figsize}\PY{o}{=}\PY{p}{(}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{transformation\PYZus{}test}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2RGB}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Original Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{transformed\PYZus{}image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2RGB}\PY{p}{)}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Transformed Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}7}]:} <matplotlib.text.Text at 0x17646dbe358>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_11_1.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{image-thresholding}{%
\subsubsection{Image Thresholding}\label{image-thresholding}}
After trying different strategies to apply thresholding finally I
applied HLS Colour space and Sobel thresholding to fecth features of
interest from the frame, here features of intrest are Lane lines. I used
Sobel along the X axis that is \texttt{sobelx} and later applied
thresholding over it, and I also applied thresholding over HLS colour
space to get a binary image. \emph{Values for Thresholding}
\begin{longtable}[]{@{}cc@{}}
\toprule
Applied On & Range\tabularnewline
\midrule
\endhead
Sobel X & 25 - 120\tabularnewline
l\_channel & 70 - 255\tabularnewline
s\_channel & 90 - 255\tabularnewline
\bottomrule
\end{longtable}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}8}]:} \PY{k}{def} \PY{n+nf}{apply\PYZus{}sobel}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{thresholds}\PY{p}{,}\PY{n}{kernel\PYZus{}size}\PY{o}{=}\PY{l+m+mi}{3}\PY{p}{,}\PY{n}{Color\PYZus{}Encoding}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{RGB}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{k}{if}\PY{p}{(}\PY{n}{Color\PYZus{}Encoding}\PY{o}{==}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{BGR}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2GRAY}\PY{p}{)}
\PY{k}{else}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}RGB2GRAY}\PY{p}{)}
\PY{n}{sobelx}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{Sobel}\PY{p}{(}\PY{n}{gray}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{CV\PYZus{}64F}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{n}{ksize}\PY{o}{=}\PY{n}{kernel\PYZus{}size}\PY{p}{)}
\PY{n}{sobel\PYZus{}scaled}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{uint8}\PY{p}{(}\PY{l+m+mi}{255}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{o}{/}\PY{n}{np}\PY{o}{.}\PY{n}{max}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros\PYZus{}like}\PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{p}{[}\PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{thresholds}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{thresholds}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{]}\PY{o}{=}\PY{l+m+mi}{1}
\PY{k}{return} \PY{n}{binary\PYZus{}image}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}9}]:} \PY{k}{def} \PY{n+nf}{apply\PYZus{}sobel\PYZus{}and\PYZus{}color\PYZus{}thresholding}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{,}\PY{n}{threshold\PYZus{}l}\PY{p}{,}\PY{n}{thresholds\PYZus{}s}\PY{p}{,}\PY{n}{kernel\PYZus{}size}\PY{o}{=}\PY{l+m+mi}{3}\PY{p}{,}\PY{n}{Color\PYZus{}Encoding}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{RGB}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{k}{if}\PY{p}{(}\PY{n}{Color\PYZus{}Encoding}\PY{o}{==}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{BGR}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2GRAY}\PY{p}{)}
\PY{n}{hls}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2HLS}\PY{p}{)}
\PY{k}{else}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}RGB2GRAY}\PY{p}{)}
\PY{n}{hls}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}RGB2HLS}\PY{p}{)}
\PY{n}{s\PYZus{}channel}\PY{o}{=}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}
\PY{n}{l\PYZus{}channel}\PY{o}{=}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}
\PY{n}{sobelx}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{Sobel}\PY{p}{(}\PY{n}{gray}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{CV\PYZus{}64F}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{n}{ksize}\PY{o}{=}\PY{n}{kernel\PYZus{}size}\PY{p}{)}
\PY{n}{scaled\PYZus{}sobel}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{uint8}\PY{p}{(}\PY{l+m+mi}{255}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{o}{/}\PY{n}{np}\PY{o}{.}\PY{n}{max}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros\PYZus{}like}\PY{p}{(}\PY{n}{scaled\PYZus{}sobel}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{p}{[}\PY{p}{(}\PY{p}{(}\PY{n}{scaled\PYZus{}sobel}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{scaled\PYZus{}sobel}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{o}{|}\PY{p}{(}\PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}s}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}s}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{threshold\PYZus{}l}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{threshold\PYZus{}l}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{]}\PY{o}{=}\PY{l+m+mi}{1}
\PY{k}{return} \PY{n}{binary\PYZus{}image}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}10}]:} \PY{k}{def} \PY{n+nf}{apply\PYZus{}sobel\PYZus{}direction}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{,}\PY{n}{thresholds\PYZus{}direction}\PY{p}{,}\PY{n}{kernel\PYZus{}size}\PY{o}{=}\PY{l+m+mi}{3}\PY{p}{,}\PY{n}{Color\PYZus{}Encoding}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{RGB}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{k}{if}\PY{p}{(}\PY{n}{Color\PYZus{}Encoding}\PY{o}{==}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{BGR}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2GRAY}\PY{p}{)}
\PY{k}{else}\PY{p}{:}
\PY{n}{gray}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}RGB2GRAY}\PY{p}{)}
\PY{n}{sobelx}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{Sobel}\PY{p}{(}\PY{n}{gray}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{CV\PYZus{}64F}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{n}{ksize}\PY{o}{=}\PY{n}{kernel\PYZus{}size}\PY{p}{)}
\PY{n}{sobely}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{Sobel}\PY{p}{(}\PY{n}{gray}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{CV\PYZus{}64F}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{ksize}\PY{o}{=}\PY{n}{kernel\PYZus{}size}\PY{p}{)}
\PY{n}{sobel\PYZus{}direction}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{arctan2}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{p}{,}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{sobely}\PY{p}{)}\PY{p}{)}
\PY{n}{sobel\PYZus{}scaled}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{uint8}\PY{p}{(}\PY{l+m+mi}{255}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{o}{/}\PY{n}{np}\PY{o}{.}\PY{n}{max}\PY{p}{(}\PY{n}{sobelx}\PY{p}{)}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros\PYZus{}like}\PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{p}{[}\PY{p}{(}\PY{n}{sobel\PYZus{}direction}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}direction}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{sobel\PYZus{}direction}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}direction}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}}\PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{sobel\PYZus{}scaled}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{thresholds\PYZus{}sobel}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{]}\PY{o}{=}\PY{l+m+mi}{1}
\PY{k}{return} \PY{n}{binary\PYZus{}image}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}11}]:} \PY{k}{def} \PY{n+nf}{color\PYZus{}thresholding}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{l\PYZus{}threshold}\PY{p}{,}\PY{n}{s\PYZus{}threshold}\PY{p}{)}\PY{p}{:}
\PY{n}{hls}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2HLS}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros\PYZus{}like}\PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{binary\PYZus{}image}\PY{p}{[}\PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{s\PYZus{}threshold}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{s\PYZus{}threshold}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{n}{l\PYZus{}threshold}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{hls}\PY{p}{[}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{\PYZlt{}}\PY{o}{=}\PY{n}{l\PYZus{}threshold}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{]}\PY{o}{=}\PY{l+m+mi}{1}
\PY{k}{return} \PY{n}{binary\PYZus{}image}
\end{Verbatim}
\hypertarget{test-thresholding}{%
\paragraph{Test Thresholding}\label{test-thresholding}}
Below is the image that my function returns after applying thresholding
over it:
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}12}]:} \PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{apply\PYZus{}sobel\PYZus{}and\PYZus{}color\PYZus{}thresholding}\PY{p}{(}\PY{n}{transformation\PYZus{}test}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{25}\PY{p}{,}\PY{l+m+mi}{120}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{70}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{90}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{]}\PY{p}{)}
\PY{n}{plt}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{binary\PYZus{}image}\PY{p}{,}\PY{n}{cmap}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{gray}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}12}]:} <matplotlib.image.AxesImage at 0x17646e6d940>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_18_1.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{thresholding-and-perspective-transform}{%
\subsubsection{Thresholding and Perspective
Transform}\label{thresholding-and-perspective-transform}}
Below is the result that my pipeline returned after applying
thresholding and later performing perspective transform:
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}13}]:} \PY{n}{transformed\PYZus{}image}\PY{o}{=}\PY{n}{perspective\PYZus{}transform}\PY{p}{(}\PY{n}{binary\PYZus{}image}\PY{p}{,}\PY{n}{transformation\PYZus{}matrix}\PY{p}{)}
\PY{n}{output\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{dstack}\PY{p}{(}\PY{p}{(}\PY{n}{transformed\PYZus{}image}\PY{p}{,}\PY{n}{transformed\PYZus{}image}\PY{p}{,}\PY{n}{transformed\PYZus{}image}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{l+m+mi}{255}
\PY{n}{f}\PY{p}{,} \PY{p}{(}\PY{n}{ax1}\PY{p}{,} \PY{n}{ax2}\PY{p}{)} \PY{o}{=} \PY{n}{plt}\PY{o}{.}\PY{n}{subplots}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{n}{figsize}\PY{o}{=}\PY{p}{(}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{cv2}\PY{o}{.}\PY{n}{cvtColor}\PY{p}{(}\PY{n}{transformation\PYZus{}test}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{COLOR\PYZus{}BGR2RGB}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Original Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Transformed \PYZam{} Thresholded Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}13}]:} <matplotlib.text.Text at 0x17645732128>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_20_1.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{pipeline}{%
\subsubsection{Pipeline}\label{pipeline}}
I used a class to retain the data from previous frames, my pipeline
function is also a member of this class. Steps performed: 1. For the
First Frame calculate Camera matrix, distortion Coefficients,
transformation matrix and inverse transformation matrix. 2. Undistort
the Image 3. Apply Thresholding 4. Take Perspective Transform 5. Find
out the pixel indices that are part of lane lines. 6. Fit the polynomial
to the left and right lane pixels. 7. Take the mean of the previous
polynomial coefficients and the coefficeint calculated in step 6. 8.
Calculate the x points for each value of Y(0-719) using the equation
AY\^{}2+BY+C. 9. Then using \texttt{cv2.fillPoly()} fill the lane line
area. 10. Take the inverse Perspective transform of the image received
from step 9. 11. Add the original image and image from step 10. 12.
Calculate Radius of curvature and deviation from center and put it over
the image received after step 12.
\hypertarget{deviation-from-center}{%
\subsubsection{Deviation from center}\label{deviation-from-center}}
To calculate the deviation from center I assumed that the center of the
image is the center of the car, then I simply calculated the center of
the lane lines and the differnce between center of the image and center
of the lane lines is required deviation.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}22}]:} \PY{k}{class} \PY{n+nc}{LaneLineFinding}\PY{p}{(}\PY{n+nb}{object}\PY{p}{)}\PY{p}{:}
\PY{c+c1}{\PYZsh{}prev\PYZus{}left\PYZus{}x=np.array([])}
\PY{c+c1}{\PYZsh{}prev\PYZus{}left\PYZus{}y=np.array([])}
\PY{c+c1}{\PYZsh{}prev\PYZus{}right\PYZus{}x=np.array([])}
\PY{c+c1}{\PYZsh{}prev\PYZus{}right\PYZus{}y=np.array([])}
\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{p}{]}\PY{p}{)}
\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{p}{]}\PY{p}{)}
\PY{n}{last\PYZus{}left\PYZus{}fit}\PY{o}{=}\PY{p}{[}\PY{p}{]}
\PY{n}{last\PYZus{}right\PYZus{}fit}\PY{o}{=}\PY{p}{[}\PY{p}{]}
\PY{n}{mtx}\PY{o}{=}\PY{k+kc}{None}
\PY{n}{dist}\PY{o}{=}\PY{k+kc}{None}
\PY{n}{trans\PYZus{}matrix}\PY{o}{=}\PY{k+kc}{None}
\PY{n}{inverse\PYZus{}trans\PYZus{}matrix}\PY{o}{=}\PY{k+kc}{None}
\PY{n}{flag}\PY{o}{=}\PY{k+kc}{True}
\PY{k}{def} \PY{n+nf}{pipeline}\PY{p}{(}\PY{n+nb+bp}{self}\PY{p}{,}\PY{n}{image}\PY{p}{)}\PY{p}{:}
\PY{k}{if}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{flag}\PY{p}{)}\PY{p}{:}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{mtx}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{dist}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{trans\PYZus{}matrix}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{inverse\PYZus{}trans\PYZus{}matrix}\PY{o}{=}\PY{n}{get\PYZus{}variables}\PY{p}{(}\PY{p}{)}
\PY{n}{undistort}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{undistort}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{mtx}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{dist}\PY{p}{,}\PY{k+kc}{None}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{mtx}\PY{p}{)}
\PY{c+c1}{\PYZsh{}binary\PYZus{}image=apply\PYZus{}sobel\PYZus{}and\PYZus{}color\PYZus{}thresholding(transformed\PYZus{}image,[12,170],[48,255])}
\PY{c+c1}{\PYZsh{}binary\PYZus{}image=color\PYZus{}thresholding(undistort,[75,255],[85,255])}
\PY{n}{binary\PYZus{}image}\PY{o}{=}\PY{n}{apply\PYZus{}sobel\PYZus{}and\PYZus{}color\PYZus{}thresholding}\PY{p}{(}\PY{n}{undistort}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{25}\PY{p}{,}\PY{l+m+mi}{120}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{70}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{]}\PY{p}{,}\PY{p}{[}\PY{l+m+mi}{90}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{]}\PY{p}{)}\PY{c+c1}{\PYZsh{}[70,120],[120,255],[90,255])\PYZsh{}[70,120],[100,255],[90,255])\PYZsh{}[25,150],[90,255],[90,255])}
\PY{n}{transformed\PYZus{}image}\PY{o}{=}\PY{n}{perspective\PYZus{}transform}\PY{p}{(}\PY{n}{binary\PYZus{}image}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{trans\PYZus{}matrix}\PY{p}{)}
\PY{k}{if}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{flag}\PY{p}{)}\PY{p}{:}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{flag}\PY{o}{=}\PY{k+kc}{False}
\PY{n}{left\PYZus{}x}\PY{p}{,}\PY{n}{left\PYZus{}y}\PY{p}{,}\PY{n}{right\PYZus{}x}\PY{p}{,}\PY{n}{right\PYZus{}y}\PY{o}{=}\PY{n}{sliding\PYZus{}window}\PY{p}{(}\PY{n}{transformed\PYZus{}image}\PY{p}{)}
\PY{k}{else}\PY{p}{:}
\PY{n}{left\PYZus{}x}\PY{p}{,}\PY{n}{left\PYZus{}y}\PY{p}{,}\PY{n}{right\PYZus{}x}\PY{p}{,}\PY{n}{right\PYZus{}y}\PY{o}{=}\PY{n}{find\PYZus{}lane\PYZus{}lines}\PY{p}{(}\PY{n}{transformed\PYZus{}image}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{last\PYZus{}left\PYZus{}fit}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{last\PYZus{}right\PYZus{}fit}\PY{p}{)}
\PY{c+c1}{\PYZsh{}output= np.dstack((transformed\PYZus{}image,np.zeros\PYZus{}like(binary\PYZus{}image),np.zeros\PYZus{}like(binary\PYZus{}image)))*255}
\PY{c+c1}{\PYZsh{}plt.imshow(binary\PYZus{}image)}
\PY{c+c1}{\PYZsh{}print(output)}
\PY{c+c1}{\PYZsh{}if(len(self.prev\PYZus{}left\PYZus{}x)\PYZgt{}=720*10):}
\PY{c+c1}{\PYZsh{} self.prev\PYZus{}left\PYZus{}x=self.prev\PYZus{}left\PYZus{}x[720:len(self.prev\PYZus{}left\PYZus{}x)]}
\PY{c+c1}{\PYZsh{}if(len(self.prev\PYZus{}right\PYZus{}x)\PYZgt{}=720*10):}
\PY{c+c1}{\PYZsh{} self.prev\PYZus{}right\PYZus{}x=self.prev\PYZus{}right\PYZus{}x[720:len(self.prev\PYZus{}right\PYZus{}x)]}
\PY{c+c1}{\PYZsh{}if(len(self.prev\PYZus{}left\PYZus{}y)\PYZgt{}=720*10):}
\PY{c+c1}{\PYZsh{} self.prev\PYZus{}left\PYZus{}y=self.prev\PYZus{}left\PYZus{}y[720:len(self.prev\PYZus{}left\PYZus{}y)]}
\PY{c+c1}{\PYZsh{}if(len(self.prev\PYZus{}right\PYZus{}y)\PYZgt{}=720*10):}
\PY{c+c1}{\PYZsh{} self.prev\PYZus{}right\PYZus{}y=self.prev\PYZus{}right\PYZus{}y[720:len(self.prev\PYZus{}right\PYZus{}y)]}
\PY{c+c1}{\PYZsh{}self.prev\PYZus{}left\PYZus{}x=np.append(self.prev\PYZus{}left\PYZus{}x,left\PYZus{}x)}
\PY{c+c1}{\PYZsh{}self.prev\PYZus{}left\PYZus{}y=np.append(self.prev\PYZus{}left\PYZus{}y,left\PYZus{}y)}
\PY{c+c1}{\PYZsh{}self.prev\PYZus{}right\PYZus{}x=np.append(self.prev\PYZus{}right\PYZus{}x,right\PYZus{}x)}
\PY{c+c1}{\PYZsh{}self.prev\PYZus{}right\PYZus{}y=np.append(self.prev\PYZus{}right\PYZus{}y,right\PYZus{}y)}
\PY{c+c1}{\PYZsh{}left\PYZus{}fit=np.polyfit(self.prev\PYZus{}left\PYZus{}y, self.prev\PYZus{}left\PYZus{}x,2)}
\PY{c+c1}{\PYZsh{}right\PYZus{}fit=np.polyfit(self.prev\PYZus{}right\PYZus{}y,self.prev\PYZus{}right\PYZus{}x,2)}
\PY{n}{left\PYZus{}fit}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{polyfit}\PY{p}{(}\PY{n}{left\PYZus{}y}\PY{p}{,} \PY{n}{left\PYZus{}x}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)}
\PY{n}{right\PYZus{}fit}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{polyfit}\PY{p}{(}\PY{n}{right\PYZus{}y}\PY{p}{,}\PY{n}{right\PYZus{}x}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)}
\PY{k}{if}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{p}{)}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{l+m+mi}{10}\PY{o}{*}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{:}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{o}{=}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{p}{)}\PY{p}{]}
\PY{k}{if}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{p}{)}\PY{o}{\PYZgt{}}\PY{o}{=}\PY{l+m+mi}{3}\PY{o}{*}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{:}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{o}{=}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{p}{)}\PY{p}{]}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{p}{,}\PY{n}{right\PYZus{}fit}\PY{p}{)}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{p}{,}\PY{n}{left\PYZus{}fit}\PY{p}{)}
\PY{n}{left\PYZus{}fit}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}left\PYZus{}coeff}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,}\PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\PY{n}{right\PYZus{}fit}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{prev\PYZus{}right\PYZus{}coeff}\PY{o}{.}\PY{n}{reshape}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,}\PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{last\PYZus{}left\PYZus{}fit}\PY{o}{=}\PY{n}{left\PYZus{}fit}
\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{last\PYZus{}right\PYZus{}fit}\PY{o}{=}\PY{n}{right\PYZus{}fit}
\PY{n}{ploty} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{linspace}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{n}{image}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,} \PY{n}{image}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} \PY{p}{)}
\PY{n}{left\PYZus{}fitx} \PY{o}{=} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{ploty}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{ploty} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]}
\PY{n}{right\PYZus{}fitx} \PY{o}{=} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{ploty}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{ploty} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]}
\PY{n}{output\PYZus{}image}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{zeros\PYZus{}like}\PY{p}{(}\PY{n}{image}\PY{p}{)}
\PY{n}{pts\PYZus{}left} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{n}{np}\PY{o}{.}\PY{n}{transpose}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{vstack}\PY{p}{(}\PY{p}{[}\PY{n}{left\PYZus{}fitx}\PY{p}{,} \PY{n}{ploty}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{]}\PY{p}{)}
\PY{n}{pts\PYZus{}right} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{n}{np}\PY{o}{.}\PY{n}{flipud}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{transpose}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{vstack}\PY{p}{(}\PY{p}{[}\PY{n}{right\PYZus{}fitx}\PY{p}{,} \PY{n}{ploty}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{)}\PY{p}{]}\PY{p}{)}
\PY{n}{pts} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{hstack}\PY{p}{(}\PY{p}{(}\PY{n}{pts\PYZus{}left}\PY{p}{,} \PY{n}{pts\PYZus{}right}\PY{p}{)}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Draw the lane onto the warped blank image}
\PY{n}{cv2}\PY{o}{.}\PY{n}{fillPoly}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{,} \PY{n}{np}\PY{o}{.}\PY{n}{int\PYZus{}}\PY{p}{(}\PY{p}{[}\PY{n}{pts}\PY{p}{]}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{255}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)}\PY{p}{)}
\PY{n}{final\PYZus{}image}\PY{o}{=}\PY{n}{perspective\PYZus{}transform}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{,}\PY{n+nb+bp}{self}\PY{o}{.}\PY{n}{inverse\PYZus{}trans\PYZus{}matrix}\PY{p}{)}
\PY{n}{output\PYZus{}image}\PY{o}{=}\PY{n}{cv2}\PY{o}{.}\PY{n}{addWeighted}\PY{p}{(}\PY{n}{image}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{n}{final\PYZus{}image}\PY{p}{,}\PY{l+m+mf}{0.5}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{)}
\PY{n}{left\PYZus{}curverad}\PY{p}{,}\PY{n}{right\PYZus{}curverad}\PY{o}{=}\PY{n}{calculate\PYZus{}radius\PYZus{}of\PYZus{}curvature}\PY{p}{(}\PY{n}{left\PYZus{}fit}\PY{p}{,}\PY{n}{right\PYZus{}fit}\PY{p}{,}\PY{n}{np}\PY{o}{.}\PY{n}{max}\PY{p}{(}\PY{n}{ploty}\PY{p}{)}\PY{p}{)}
\PY{n}{cv2}\PY{o}{.}\PY{n}{putText}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Radius Of Curvature = }\PY{l+s+si}{\PYZob{}0:.2f\PYZcb{}}\PY{l+s+s1}{(m)}\PY{l+s+s1}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{p}{[}\PY{n}{left\PYZus{}curverad}\PY{p}{,}\PY{n}{right\PYZus{}curverad}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{50}\PY{p}{,}\PY{l+m+mi}{50}\PY{p}{)}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{LINE\PYZus{}AA}\PY{p}{)}
\PY{n}{deviation\PYZus{}from\PYZus{}center}\PY{o}{=}\PY{p}{(}\PY{p}{(}\PY{n}{image}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{p}{(}\PY{p}{(}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{right\PYZus{}fitx}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{\PYZhy{}}\PY{n}{left\PYZus{}fitx}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{)}\PY{o}{+}\PY{n}{left\PYZus{}fitx}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{p}{(}\PY{l+m+mf}{3.7}\PY{o}{/}\PY{l+m+mi}{600}\PY{p}{)}
\PY{k}{if}\PY{p}{(}\PY{n}{deviation\PYZus{}from\PYZus{}center}\PY{o}{\PYZgt{}}\PY{l+m+mi}{0}\PY{p}{)}\PY{p}{:}
\PY{n}{cv2}\PY{o}{.}\PY{n}{putText}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{vehicle is }\PY{l+s+si}{\PYZob{}0:.2f\PYZcb{}}\PY{l+s+s1}{(m) left from the center}\PY{l+s+s1}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{deviation\PYZus{}from\PYZus{}center}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{50}\PY{p}{,}\PY{l+m+mi}{100}\PY{p}{)}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{LINE\PYZus{}AA}\PY{p}{)}
\PY{k}{else}\PY{p}{:}
\PY{n}{cv2}\PY{o}{.}\PY{n}{putText}\PY{p}{(}\PY{n}{output\PYZus{}image}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{vehicle is }\PY{l+s+si}{\PYZob{}0:.2f\PYZcb{}}\PY{l+s+s1}{(m) right from the center}\PY{l+s+s1}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{n}{deviation\PYZus{}from\PYZus{}center}\PY{p}{)}\PY{p}{)}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{50}\PY{p}{,}\PY{l+m+mi}{100}\PY{p}{)}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{,}\PY{l+m+mi}{255}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{n}{cv2}\PY{o}{.}\PY{n}{LINE\PYZus{}AA}\PY{p}{)}
\PY{k}{return} \PY{n}{output\PYZus{}image}
\end{Verbatim}
\hypertarget{sliding-window}{%
\subsubsection{Sliding Window}\label{sliding-window}}
I have used sliding window approach to find out the indices of the
pixels those are part of the lane lines. This approach uses a window and
only considers nonzero pixels within that window, in my implementaion I
have used 9 windows hence there will be 9 windows for left lane and 9
windows for right lane, each of 80 pixel height and 200 pixels width.
This function returns the \texttt{left\_x},\texttt{left\_y} for left
lane and \texttt{right\_x},\texttt{right\_y} for right lane. These
points I used in Pipeline to fit a polynomial.
Sliding window is not being used by each frame, since consecutive frames
will not have much difference in lane line positions, hence we are
serching in a given margin for the next frame say 100 in my case. I have
defined \texttt{find\_lane\_lines()} for this purpose this function also
returns the \texttt{left\_x},\texttt{left\_y} for left lane and
\texttt{right\_x},\texttt{right\_y} for right lane. These points I used
in Pipeline to fit a polynomial.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}15}]:} \PY{k}{def} \PY{n+nf}{sliding\PYZus{}window}\PY{p}{(}\PY{n}{binary\PYZus{}warped}\PY{p}{,}\PY{n}{visualize}\PY{o}{=}\PY{k+kc}{False}\PY{p}{)}\PY{p}{:}
\PY{n}{histogram}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{binary\PYZus{}warped}\PY{p}{[}\PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{/}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{:}\PY{p}{,}\PY{p}{:}\PY{p}{]}\PY{p}{,}\PY{n}{axis}\PY{o}{=}\PY{l+m+mi}{0}\PY{p}{)}
\PY{n}{out\PYZus{}img} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{dstack}\PY{p}{(}\PY{p}{(}\PY{n}{binary\PYZus{}warped}\PY{p}{,} \PY{n}{binary\PYZus{}warped}\PY{p}{,} \PY{n}{binary\PYZus{}warped}\PY{p}{)}\PY{p}{)}\PY{o}{*}\PY{l+m+mi}{255}
\PY{c+c1}{\PYZsh{} Find the peak of the left and right halves of the histogram}
\PY{c+c1}{\PYZsh{} These will be the starting point for the left and right lines}
\PY{n}{midpoint} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{int}\PY{p}{(}\PY{n}{histogram}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{/}\PY{o}{/}\PY{l+m+mi}{2}\PY{p}{)}
\PY{n}{leftx\PYZus{}base} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{argmax}\PY{p}{(}\PY{n}{histogram}\PY{p}{[}\PY{p}{:}\PY{n}{midpoint}\PY{p}{]}\PY{p}{)}
\PY{n}{rightx\PYZus{}base} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{argmax}\PY{p}{(}\PY{n}{histogram}\PY{p}{[}\PY{n}{midpoint}\PY{p}{:}\PY{p}{]}\PY{p}{)} \PY{o}{+} \PY{n}{midpoint}
\PY{c+c1}{\PYZsh{} Choose the number of sliding windows}
\PY{n}{nwindows} \PY{o}{=} \PY{l+m+mi}{9}
\PY{c+c1}{\PYZsh{} Set height of windows}
\PY{n}{window\PYZus{}height} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{int}\PY{p}{(}\PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{/}\PY{o}{/}\PY{n}{nwindows}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Identify the x and y positions of all nonzero pixels in the image}
\PY{n}{nonzero} \PY{o}{=} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{nonzero}\PY{p}{(}\PY{p}{)}
\PY{n}{nonzeroy} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{n}{nonzero}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{nonzerox} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{n}{nonzero}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Current positions to be updated for each window}
\PY{n}{leftx\PYZus{}current} \PY{o}{=} \PY{n}{leftx\PYZus{}base}
\PY{n}{rightx\PYZus{}current} \PY{o}{=} \PY{n}{rightx\PYZus{}base}
\PY{c+c1}{\PYZsh{} Set the width of the windows +/\PYZhy{} margin}
\PY{n}{margin} \PY{o}{=} \PY{l+m+mi}{100}
\PY{c+c1}{\PYZsh{} Set minimum number of pixels found to recenter window}
\PY{n}{minpix} \PY{o}{=} \PY{l+m+mi}{50}
\PY{c+c1}{\PYZsh{} Create empty lists to receive left and right lane pixel indices}
\PY{n}{left\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{p}{[}\PY{p}{]}
\PY{n}{right\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{p}{[}\PY{p}{]}
\PY{c+c1}{\PYZsh{} Step through the windows one by one}
\PY{k}{for} \PY{n}{window} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{n}{nwindows}\PY{p}{)}\PY{p}{:}
\PY{c+c1}{\PYZsh{} Identify window boundaries in x and y (and right and left)}
\PY{n}{win\PYZus{}y\PYZus{}low} \PY{o}{=} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} \PY{o}{\PYZhy{}} \PY{p}{(}\PY{n}{window}\PY{o}{+}\PY{l+m+mi}{1}\PY{p}{)}\PY{o}{*}\PY{n}{window\PYZus{}height}
\PY{n}{win\PYZus{}y\PYZus{}high} \PY{o}{=} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} \PY{o}{\PYZhy{}} \PY{n}{window}\PY{o}{*}\PY{n}{window\PYZus{}height}
\PY{n}{win\PYZus{}xleft\PYZus{}low} \PY{o}{=} \PY{n}{leftx\PYZus{}current} \PY{o}{\PYZhy{}} \PY{n}{margin}
\PY{n}{win\PYZus{}xleft\PYZus{}high} \PY{o}{=} \PY{n}{leftx\PYZus{}current} \PY{o}{+} \PY{n}{margin}
\PY{n}{win\PYZus{}xright\PYZus{}low} \PY{o}{=} \PY{n}{rightx\PYZus{}current} \PY{o}{\PYZhy{}} \PY{n}{margin}
\PY{n}{win\PYZus{}xright\PYZus{}high} \PY{o}{=} \PY{n}{rightx\PYZus{}current} \PY{o}{+} \PY{n}{margin}
\PY{c+c1}{\PYZsh{} Draw the windows on the visualization image}
\PY{c+c1}{\PYZsh{}cv2.rectangle(out\PYZus{}img,(win\PYZus{}xleft\PYZus{}low,win\PYZus{}y\PYZus{}low),(win\PYZus{}xleft\PYZus{}high,win\PYZus{}y\PYZus{}high),}
\PY{c+c1}{\PYZsh{}(0,255,0), 2) }
\PY{c+c1}{\PYZsh{}cv2.rectangle(out\PYZus{}img,(win\PYZus{}xright\PYZus{}low,win\PYZus{}y\PYZus{}low),(win\PYZus{}xright\PYZus{}high,win\PYZus{}y\PYZus{}high),}
\PY{c+c1}{\PYZsh{}(0,255,0), 2) }
\PY{c+c1}{\PYZsh{} Identify the nonzero pixels in x and y within the window}
\PY{n}{good\PYZus{}left\PYZus{}inds} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{n}{nonzeroy} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{win\PYZus{}y\PYZus{}low}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzeroy} \PY{o}{\PYZlt{}} \PY{n}{win\PYZus{}y\PYZus{}high}\PY{p}{)} \PY{o}{\PYZam{}}
\PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{win\PYZus{}xleft\PYZus{}low}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZlt{}} \PY{n}{win\PYZus{}xleft\PYZus{}high}\PY{p}{)}\PY{p}{)}\PY{o}{.}\PY{n}{nonzero}\PY{p}{(}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}
\PY{n}{good\PYZus{}right\PYZus{}inds} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{n}{nonzeroy} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{win\PYZus{}y\PYZus{}low}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzeroy} \PY{o}{\PYZlt{}} \PY{n}{win\PYZus{}y\PYZus{}high}\PY{p}{)} \PY{o}{\PYZam{}}
\PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{win\PYZus{}xright\PYZus{}low}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZlt{}} \PY{n}{win\PYZus{}xright\PYZus{}high}\PY{p}{)}\PY{p}{)}\PY{o}{.}\PY{n}{nonzero}\PY{p}{(}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}
\PY{c+c1}{\PYZsh{} Append these indices to the lists}
\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{good\PYZus{}left\PYZus{}inds}\PY{p}{)}
\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{good\PYZus{}right\PYZus{}inds}\PY{p}{)}
\PY{c+c1}{\PYZsh{} If you found \PYZgt{} minpix pixels, recenter next window on their mean position}
\PY{k}{if} \PY{n+nb}{len}\PY{p}{(}\PY{n}{good\PYZus{}left\PYZus{}inds}\PY{p}{)} \PY{o}{\PYZgt{}} \PY{n}{minpix}\PY{p}{:}
\PY{n}{leftx\PYZus{}current} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{int}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{n}{nonzerox}\PY{p}{[}\PY{n}{good\PYZus{}left\PYZus{}inds}\PY{p}{]}\PY{p}{)}\PY{p}{)}
\PY{k}{if} \PY{n+nb}{len}\PY{p}{(}\PY{n}{good\PYZus{}right\PYZus{}inds}\PY{p}{)} \PY{o}{\PYZgt{}} \PY{n}{minpix}\PY{p}{:}
\PY{n}{rightx\PYZus{}current} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{int}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{mean}\PY{p}{(}\PY{n}{nonzerox}\PY{p}{[}\PY{n}{good\PYZus{}right\PYZus{}inds}\PY{p}{]}\PY{p}{)}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Concatenate the arrays of indices}
\PY{n}{left\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{)}
\PY{n}{right\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Extract left and right line pixel positions}
\PY{n}{leftx} \PY{o}{=} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{lefty} \PY{o}{=} \PY{n}{nonzeroy}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{rightx} \PY{o}{=} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{righty} \PY{o}{=} \PY{n}{nonzeroy}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{k}{if}\PY{p}{(}\PY{n}{visualize}\PY{p}{)}\PY{p}{:}
\PY{c+c1}{\PYZsh{} Fit a second order polynomial to each}
\PY{n}{left\PYZus{}fit} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{polyfit}\PY{p}{(}\PY{n}{lefty}\PY{p}{,} \PY{n}{leftx}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}
\PY{n}{right\PYZus{}fit} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{polyfit}\PY{p}{(}\PY{n}{righty}\PY{p}{,} \PY{n}{rightx}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}
\PY{n}{ploty} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{linspace}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} \PY{p}{)}
\PY{n}{left\PYZus{}fitx} \PY{o}{=} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{ploty}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{ploty} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]}
\PY{n}{right\PYZus{}fitx} \PY{o}{=} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{ploty}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{ploty} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]}
\PY{n}{out\PYZus{}img}\PY{p}{[}\PY{n}{nonzeroy}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}\PY{p}{,} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}\PY{p}{]} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{255}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}
\PY{n}{out\PYZus{}img}\PY{p}{[}\PY{n}{nonzeroy}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}\PY{p}{,} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}\PY{p}{]} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{255}\PY{p}{]}
\PY{n}{plt}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{out\PYZus{}img}\PY{p}{)}
\PY{n}{plt}\PY{o}{.}\PY{n}{plot}\PY{p}{(}\PY{n}{left\PYZus{}fitx}\PY{p}{,} \PY{n}{ploty}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{yellow}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{plt}\PY{o}{.}\PY{n}{plot}\PY{p}{(}\PY{n}{right\PYZus{}fitx}\PY{p}{,} \PY{n}{ploty}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{yellow}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{plt}\PY{o}{.}\PY{n}{xlim}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1280}\PY{p}{)}
\PY{n}{plt}\PY{o}{.}\PY{n}{ylim}\PY{p}{(}\PY{l+m+mi}{720}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{)}
\PY{k}{return} \PY{k+kc}{None}
\PY{k}{return} \PY{n}{leftx}\PY{p}{,}\PY{n}{lefty}\PY{p}{,}\PY{n}{rightx}\PY{p}{,}\PY{n}{righty}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}16}]:} \PY{k}{def} \PY{n+nf}{find\PYZus{}lane\PYZus{}lines}\PY{p}{(}\PY{n}{binary\PYZus{}warped}\PY{p}{,}\PY{n}{left\PYZus{}fit}\PY{p}{,}\PY{n}{right\PYZus{}fit}\PY{p}{)}\PY{p}{:}
\PY{n}{nonzero} \PY{o}{=} \PY{n}{binary\PYZus{}warped}\PY{o}{.}\PY{n}{nonzero}\PY{p}{(}\PY{p}{)}
\PY{n}{nonzeroy} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{n}{nonzero}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{nonzerox} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{n}{nonzero}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}
\PY{n}{margin} \PY{o}{=} \PY{l+m+mi}{100}
\PY{n}{left\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZgt{}} \PY{p}{(}\PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{p}{(}\PY{n}{nonzeroy}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{nonzeroy} \PY{o}{+}
\PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]} \PY{o}{\PYZhy{}} \PY{n}{margin}\PY{p}{)}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZlt{}} \PY{p}{(}\PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{p}{(}\PY{n}{nonzeroy}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+}
\PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{nonzeroy} \PY{o}{+} \PY{n}{left\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]} \PY{o}{+} \PY{n}{margin}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{right\PYZus{}lane\PYZus{}inds} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZgt{}} \PY{p}{(}\PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{p}{(}\PY{n}{nonzeroy}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{nonzeroy} \PY{o}{+}
\PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]} \PY{o}{\PYZhy{}} \PY{n}{margin}\PY{p}{)}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{nonzerox} \PY{o}{\PYZlt{}} \PY{p}{(}\PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{p}{(}\PY{n}{nonzeroy}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)} \PY{o}{+}
\PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{*}\PY{n}{nonzeroy} \PY{o}{+} \PY{n}{right\PYZus{}fit}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]} \PY{o}{+} \PY{n}{margin}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Again, extract left and right line pixel positions}
\PY{n}{leftx} \PY{o}{=} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{lefty} \PY{o}{=} \PY{n}{nonzeroy}\PY{p}{[}\PY{n}{left\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{rightx} \PY{o}{=} \PY{n}{nonzerox}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{n}{righty} \PY{o}{=} \PY{n}{nonzeroy}\PY{p}{[}\PY{n}{right\PYZus{}lane\PYZus{}inds}\PY{p}{]}
\PY{c+c1}{\PYZsh{} Fit a second order polynomial to each}
\PY{c+c1}{\PYZsh{}left\PYZus{}fit = np.polyfit(lefty, leftx, 2)}
\PY{c+c1}{\PYZsh{}right\PYZus{}fit = np.polyfit(righty, rightx, 2)}
\PY{c+c1}{\PYZsh{} Generate x and y values for plotting}
\PY{c+c1}{\PYZsh{}ploty = np.linspace(0, binary\PYZus{}warped.shape[0]\PYZhy{}1, binary\PYZus{}warped.shape[0] )}
\PY{c+c1}{\PYZsh{}left\PYZus{}fitx = left\PYZus{}fit[0]*ploty**2 + left\PYZus{}fit[1]*ploty + left\PYZus{}fit[2]}
\PY{c+c1}{\PYZsh{}right\PYZus{}fitx = right\PYZus{}fit[0]*ploty**2 + right\PYZus{}fit[1]*ploty + right\PYZus{}fit[2]}
\PY{k}{return} \PY{n}{leftx}\PY{p}{,}\PY{n}{lefty}\PY{p}{,}\PY{n}{rightx}\PY{p}{,}\PY{n}{righty}
\end{Verbatim}
\hypertarget{fit-the-polynomial}{%
\paragraph{Fit the Polynomial}\label{fit-the-polynomial}}
Below is the result after identifying the lane line points and fitting a
polynomial to them.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}17}]:} \PY{n}{sliding\PYZus{}window}\PY{p}{(}\PY{n}{transformed\PYZus{}image}\PY{p}{,}\PY{n}{visualize}\PY{o}{=}\PY{k+kc}{True}\PY{p}{)}
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_27_0.png}
\end{center}
{ \hspace*{\fill} \\}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}18}]:} \PY{k}{def} \PY{n+nf}{get\PYZus{}variables}\PY{p}{(}\PY{p}{)}\PY{p}{:}
\PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{o}{=}\PY{n}{calibrate\PYZus{}camera}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./camera\PYZus{}cal/calibration*.jpg}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{c+c1}{\PYZsh{}1src\PYZus{}points=np.float32([[800,470],[1100,720],[210,720],[500,470]])}
\PY{c+c1}{\PYZsh{}1dest\PYZus{}points=np.float32([[1100,0],[1100,720],[200,720],[200,0]])}
\PY{c+c1}{\PYZsh{}src\PYZus{}points=np.float32([[790,490],[1090,670],[250,670],[515,490]])}
\PY{c+c1}{\PYZsh{}dest\PYZus{}points=np.float32([[1100,0],[1100,720],[200,720],[200,0]])}
\PY{n}{src\PYZus{}points} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{float32}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{200}\PY{p}{,}\PY{l+m+mi}{720}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{600}\PY{p}{,} \PY{l+m+mi}{447}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{679}\PY{p}{,}\PY{l+m+mi}{447}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{1100}\PY{p}{,}\PY{l+m+mi}{720}\PY{p}{]}\PY{p}{]}\PY{p}{)}
\PY{n}{dest\PYZus{}points} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{float32}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{300}\PY{p}{,} \PY{l+m+mi}{720}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{300}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{900}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{900}\PY{p}{,} \PY{l+m+mi}{720}\PY{p}{]}\PY{p}{]}\PY{p}{)}
\PY{n}{transformation\PYZus{}matrix}\PY{o}{=}\PY{n}{get\PYZus{}transformation\PYZus{}matrix}\PY{p}{(}\PY{n}{src\PYZus{}points}\PY{p}{,}\PY{n}{dest\PYZus{}points}\PY{p}{)}
\PY{n}{inverse\PYZus{}transformation\PYZus{}matrix}\PY{o}{=}\PY{n}{get\PYZus{}transformation\PYZus{}matrix}\PY{p}{(}\PY{n}{dest\PYZus{}points}\PY{p}{,}\PY{n}{src\PYZus{}points}\PY{p}{)}
\PY{k}{return} \PY{n}{mtx}\PY{p}{,}\PY{n}{dist}\PY{p}{,}\PY{n}{transformation\PYZus{}matrix}\PY{p}{,}\PY{n}{inverse\PYZus{}transformation\PYZus{}matrix}
\end{Verbatim}
\hypertarget{radius-of-curvature}{%
\subsubsection{Radius of curvature}\label{radius-of-curvature}}
Once we have the polynomial coefficients we can use them to find out the
radius of curvature. I have scaled the Radius of curvature in the
meters.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}19}]:} \PY{k}{def} \PY{n+nf}{calculate\PYZus{}radius\PYZus{}of\PYZus{}curvature}\PY{p}{(}\PY{n}{left\PYZus{}fit\PYZus{}cr}\PY{p}{,}\PY{n}{right\PYZus{}fit\PYZus{}cr}\PY{p}{,}\PY{n}{y\PYZus{}eval}\PY{p}{)}\PY{p}{:}
\PY{n}{ym\PYZus{}per\PYZus{}pix} \PY{o}{=} \PY{l+m+mi}{40}\PY{o}{/}\PY{l+m+mi}{720} \PY{c+c1}{\PYZsh{} meters per pixel in y dimension}
\PY{n}{xm\PYZus{}per\PYZus{}pix} \PY{o}{=} \PY{l+m+mf}{3.7}\PY{o}{/}\PY{l+m+mi}{600} \PY{c+c1}{\PYZsh{} meters per pixel in x dimension}
\PY{c+c1}{\PYZsh{} Fit new polynomials to x,y in world space}
\PY{c+c1}{\PYZsh{}left\PYZus{}fit\PYZus{}cr = np.polyfit(ploty*ym\PYZus{}per\PYZus{}pix, leftx*xm\PYZus{}per\PYZus{}pix, 2)}
\PY{c+c1}{\PYZsh{}right\PYZus{}fit\PYZus{}cr = np.polyfit(ploty*ym\PYZus{}per\PYZus{}pix, rightx*xm\PYZus{}per\PYZus{}pix, 2)}
\PY{c+c1}{\PYZsh{} Calculate the new radii of curvature}
\PY{n}{left\PYZus{}curverad} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{l+m+mi}{1} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{left\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{y\PYZus{}eval}\PY{o}{*}\PY{n}{ym\PYZus{}per\PYZus{}pix} \PY{o}{+} \PY{n}{left\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mf}{1.5}\PY{p}{)} \PY{o}{/} \PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{left\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{n}{right\PYZus{}curverad} \PY{o}{=} \PY{p}{(}\PY{p}{(}\PY{l+m+mi}{1} \PY{o}{+} \PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{right\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{o}{*}\PY{n}{y\PYZus{}eval}\PY{o}{*}\PY{n}{ym\PYZus{}per\PYZus{}pix} \PY{o}{+} \PY{n}{right\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{)}\PY{o}{*}\PY{o}{*}\PY{l+m+mf}{1.5}\PY{p}{)} \PY{o}{/} \PY{n}{np}\PY{o}{.}\PY{n}{absolute}\PY{p}{(}\PY{l+m+mi}{2}\PY{o}{*}\PY{n}{right\PYZus{}fit\PYZus{}cr}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Now our radius of curvature is in meters}
\PY{k}{return} \PY{n}{left\PYZus{}curverad}\PY{p}{,}\PY{n}{right\PYZus{}curverad}
\end{Verbatim}
\hypertarget{pipeline-output}{%
\subsubsection{Pipeline Output}\label{pipeline-output}}
Below is the example of Pipeline output for a frame.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}23}]:} \PY{k+kn}{import} \PY{n+nn}{matplotlib}\PY{n+nn}{.}\PY{n+nn}{image} \PY{k}{as} \PY{n+nn}{matimage}
\PY{n}{test}\PY{o}{=}\PY{n}{LaneLineFinding}\PY{p}{(}\PY{p}{)}
\PY{n}{test\PYZus{}image}\PY{o}{=}\PY{n}{matimage}\PY{o}{.}\PY{n}{imread}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{./test\PYZus{}images/straight\PYZus{}lines1.jpg}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\PY{n}{test\PYZus{}output}\PY{o}{=}\PY{n}{test}\PY{o}{.}\PY{n}{pipeline}\PY{p}{(}\PY{n}{test\PYZus{}image}\PY{p}{)}
\PY{n}{f}\PY{p}{,} \PY{p}{(}\PY{n}{ax1}\PY{p}{,} \PY{n}{ax2}\PY{p}{)} \PY{o}{=} \PY{n}{plt}\PY{o}{.}\PY{n}{subplots}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{n}{figsize}\PY{o}{=}\PY{p}{(}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{10}\PY{p}{)}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{test\PYZus{}image}\PY{p}{)}
\PY{n}{ax1}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Original Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{imshow}\PY{p}{(}\PY{n}{test\PYZus{}output}\PY{p}{)}
\PY{n}{ax2}\PY{o}{.}\PY{n}{set\PYZus{}title}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Final Image}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{fontsize}\PY{o}{=}\PY{l+m+mi}{30}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}23}]:} <matplotlib.text.Text at 0x1764c5c9780>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_32_1.png}
\end{center}
{ \hspace*{\fill} \\}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}24}]:} \PY{k+kn}{from} \PY{n+nn}{moviepy}\PY{n+nn}{.}\PY{n+nn}{editor} \PY{k}{import} \PY{n}{VideoFileClip}
\PY{k+kn}{from} \PY{n+nn}{IPython}\PY{n+nn}{.}\PY{n+nn}{display} \PY{k}{import} \PY{n}{HTML}
\PY{n}{video\PYZus{}processor}\PY{o}{=}\PY{n}{LaneLineFinding}\PY{p}{(}\PY{p}{)}
\PY{n}{white\PYZus{}output} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{project\PYZus{}video\PYZus{}output.mp4}\PY{l+s+s1}{\PYZsq{}}
\PY{n}{clip1} \PY{o}{=} \PY{n}{VideoFileClip}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{project\PYZus{}video.mp4}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{white\PYZus{}clip} \PY{o}{=} \PY{n}{clip1}\PY{o}{.}\PY{n}{fl\PYZus{}image}\PY{p}{(}\PY{n}{video\PYZus{}processor}\PY{o}{.}\PY{n}{pipeline}\PY{p}{)} \PY{c+c1}{\PYZsh{}NOTE: this function expects color images!!}
\PY{o}{\PYZpc{}}\PY{k}{time} white\PYZus{}clip.write\PYZus{}videofile(white\PYZus{}output, audio=False)
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
[MoviePy] >>>> Building video project\_video\_output.mp4
[MoviePy] Writing video project\_video\_output.mp4
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
100\%|█████████████████████████████████████████████████████████████████████████████▉| 1260/1261 [03:14<00:00, 7.79it/s]
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
[MoviePy] Done.
[MoviePy] >>>> Video ready: project\_video\_output.mp4
Wall time: 3min 16s
\end{Verbatim}
% Add a bibliography block to the postdoc
\end{document}