-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp4.html
1157 lines (1016 loc) · 43.3 KB
/
p4.html
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Code 2 | Projects</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- import p5 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.min.js"></script>
<!-- import clmtrkr -->
<script src="https://www.auduno.com/clmtrackr/build/clmtrackr.min.js"></script>
<!-- import the webpage's stylesheet -->
<link rel="stylesheet" href="style.css" />
<!-- import the webpage's javascript + jQuery file -->
<script src="/script.js" defer></script>
<script src="jquery-3.4.1.min.js"></script>
</head>
<body>
<div class="nav">
<h1>Code 2</h1>
<ul>
<li>
<a
href="https://docs.google.com/document/d/16yNNHgZIqe3B8W9TLSEBrOSo2KsAF5qVFqkZ8sEh2mc/edit?usp=sharing"
target="blank"
>Syllabus</a
>
</li>
<li>
Projects <a href="p1.html">#1</a> <a href="p2.html">#2</a>
<a href="p3.html">#3</a> <a href="p4.html">#4</a> <a href="p5.html">#5</a>
</li>
<li><a href="events.html">Public Events</a></li>
<li><a href="readings.html">References</a></li>
<!-- <li><a href="people.html">People</a></li> -->
</ul>
</div>
<div class="content">
<h1>Project #4 Experimental Camera</h1>
<div class="full-width-box">
<img
src="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2FP_4_3_3_02.png?v=1583963661596"
/>
</div>
<p>
Try the camera out
<a href="https://editor.p5js.org/generative-design/sketches/P_4_3_3_02"
>here</a
>
</p>
<h2>Description</h2>
<p>
What does it mean to design a camera that is meant to capture a specific
person, place, or object in mind? <br />
What does it mean to make a camera that is meant to capture bodies that
are living under the coronavirus crisis?<br />
How do we want to document our domestic life while we self-quarantine?
<br />
How can camera filters change the way we perceive ourselves and the
others? <br />
<br />
For project #4, you will design and create an experimental camera with a
specific purpose and usage in mind. Make an intentional choice on
whether this camera will be used on a desktop or on a mobile device. The
camera should inspire the user to capture images in new and surprising
ways. You are expected to interact with the DOM, and use
createGraphics() and / or loadPixels() in your camera design.
</p>
<h2>Required Readings</h2>
<p>
(1)
<a href="https://thenewinquiry.com/closing-the-loop/"
>Closing the Loop</a
>, Aria Dean<br />
(2)
<a
href="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2FGenerative_Design_Revised_Visualize%2C_Program%2C_and_..._----_(Pg_188--225).pdf?v=1583955406388"
>Generative Design: Image (excerpt)</a
>, accomodated by
<a href="http://www.generative-gestaltung.de/2/#sketches">p5 examples</a
>, Benedikt Gross, Julia Laub, Claudius Lazzeroni, Hartmut Bohnacker
</p>
<h2>
Further Readings
</h2>
<p>
(1)
<a
href="https://medium.com/@Alibaba_Cloud/image-recognition-using-edge-detection-44bd3dc4caa"
>Image Recognition Using Edge Detection</a
>, Alibaba Cloud
<br />
(2)
<a
href="https://www.independent.co.uk/life-style/gadgets-and-tech/news/iphone-x-8-apple-features-face-id-a7944646.html"
>iPhone X Facial Recognition: How Apple's New Face ID Actually
Works</a
>, Andrew Griffin<br />
</p>
<h2>References</h2>
<p>
(1)
<a href="http://www.katehollenbach.com/user-is-present/"
>User is Present</a
>
, Kate Hollenbach
<br />
(2)
<a href="https://cyberwitch666.github.io/Gaze-Makes-The-Glitch/"
>Gaze Makes the Glitch</a
>, Cyber Witch
<br />
(3)
<a href="https://shrub.ooo/">Shrub</a>, Linked by Air & Jeffrey Scudder
<br />
(4)
<a href="https://zachblas.info/works/facial-weaponization-suite/"
>Facial Weaponization Suite</a
>, Zach Blas
<br />
(5)
<a href="https://adamferriss.com/merge/">Merge</a> /
<a href="https://adamferriss.com/multix/">Multix</a> /
<a href="https://www.adamferriss.com/sheeting/">Sheets</a> /
<a href="https://www.adamferriss.com/gush/">Gush</a>, Adam Ferriss
<br />
(6) <a href="https://glanceback.info/">Glance Back</a>, Maya Man
<br/>
(7)<a href="https://experiments.withgoogle.com/collection/ar">AR Experiments</a>, multiple creators
</p>
<h2>Resources</h2>
<p>
(1)
<a
href="https://codeburst.io/instagram-filters-with-javascript-p5-js-83f28c9f7fda"
>Instagram Filters with p5.js</a
>, Kelly Lougheed
<br />
(2)
<a href="https://github.com/processing/p5.js/wiki/Beyond-the-canvas"
>Beyond the Canvas</a
>, Evelyn Masso
<br />
(3)
<a
href="https://www.youtube.com/watch?v=0V3uYA1hafk&list=PLRqwX-V7Uu6aKKsDHZdDvN6oCJ2hRY_Ig&index=6"
>Video and Pixels Playlist</a
>, Coding Train
<br />
(4)
<a href="https://gist.github.com/lmccart/2273a047874939ad8ad1"
>Creating face filters with clmtrackr.js + p5.js</a
>, Lauren McCarthy <br />
(5)
<a href="https://kylemcdonald.github.io/cv-examples/"
>Computer vision examples</a
>, Kyle McDonald
<br />
(6)
<a href="https://www.auduno.com/clmtrackr/docs/reference.html"
>clmtrackr.js References</a
>, Audun Mathias Øygard
<br />
(7)
<a href="https://itp-xstory.github.io/p5js-shaders/#/"
>p5.js Shaders Tutorial</a
>, Casey Conchinha and Louise Lessél
<br />
(8)
<a href="https://www.nautilists.com/lists/JNI1oOq6ce"
>Pixel Manipulation with p5.js: Build your own video mirror!</a
>, jiwon
</p>
<h2>Timeline & Deliverables</h2>
<p>
March 25: Submit proposal for your exprimental camera to Canvas. The
proposal should include an overall description that describes the goals
of your experimental camera and an illustration of the interface design.
<br />
April 1: Complete white-boxed version of the project.
<br />
April 15: Project due, present during class.
</p>
<h2>Submission Guidelines</h2>
<p>
Submit proposal and white-boxed version of your project to Canvas
<br />
Submit your finalized sketch to Canvas as well as the Project #4 Glitch
page for presentation purposes
</p>
<h2>Design Constraints</h2>
<p>
(1) Your canvas size should be windowWidth * windowHeight <br />
(2) Your project should be either desktop or mobile-friendly <br />
(3) Your project should interact with the DOM<br />
(3) Your project should use createGraphics() and / or loadPixels()<br />
</p>
<h1>Study Guide</h1>
<h2>
Table of Content
</h2>
<p>
(1) <a href="#dom">Interacting with the DOM</a><br />
(2) <a href="#clm">clmtrkr.js</a><br />
(3) <a href="#cg">createGraphic()</a><br />
(4) <a href="#copy">copy()</a><br />
(5) <a href="#setget">set() and get()</a><br />
(6) <a href="#pixel">loadPixels() and updatePixels()</a><br />
(7) <a href="#perf">Performance Management</a><br />
</p>
<div id="dom"><h2>Interacting with the DOM</h2></div>
<p>
Whenever a web page is loaded, the browser creates a Document Object
Model (DOM) for the loaded page. DOM arranges all the elements on the
web page into a tree diagram —
</p>
<div class="full-width-box">
<img src="https://www.w3schools.com/js/pic_htmltree.gif" />
</div>
<p>
And the amazing part is, we can interact with the elements on this tree
diagram from our sketch.js in the p5 editor! If you visit the p5
refrence's
<a href="https://p5js.org/reference/#group-DOM">DOM section</a>, you
will find a number of DOM elements that are ready to be loaded into your
sketch. Elements that are relevant to this project are:
</p>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createCapture"
>createCapture()</a
>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createButton"
>createButton()</a
>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createSlider"
>createSlider()</a
>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createRadio">createRadio()</a>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createSelect"
>createSelect()</a
>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createCheckbox"
>createCheckbox()</a
>
</li>
<li class="bullet">
<a href="https://p5js.org/reference/#/p5/createColorPicker"
>createColorPicker()</a
>
</li>
<h3>
createCapture()
</h3>
<p>
The most important DOM element we need for Project #4 is
<a href="https://p5js.org/reference/#/p5/createCapture"
>createCapture()</a
>. Essentially createCapture() lets you access the user's webcam through
the browser. The code to load the webcam is very simple, however it is
important to remember that you should put createCapture() inside
setup(). Putting it in draw() will create a new capture every frame and
crash the program!
</p>
<script src="https://gist.github.com/xinemata/93810659cf3e1086de3bfd46a8f52db8.js"></script>
<p>
When you hit play, your browser should ask for your permission to access
your web cam. Once you accept it, you should see your image stacked
below the p5 canvas.
</p>
<p>
Web cam is a highly sensitive device that comes along with layers of
security measures. We wouldn't normally want any website to just turn on
our webcam without asking for our consent. If your web cam isn't turning
on inside the browser, a lot of times it's because you need to go into
your browser setting and give permission to specific websites to access
the web cam. This is true for both laptops and mobile devices. The
sercurity measurements on mobile devices tend to be stricter than
laptops, and iOS tends to be stricter than Androids.
</p>
<p>
Next, let's make the video feed appear inside of our canvas instead of
below. In order to do that, we can actually feed the
<i>capture</i> variable directly into the
<a href="https://p5js.org/reference/#/p5/image">image function</a>. And
just for fun, I'm also going to use one of the p5
<a href="https://p5js.org/reference/#/p5/filter">filter functions</a> to
invert the video feed:
</p>
<script src="https://gist.github.com/xinemata/9898136c4174e84ce07caff46d7b1818.js"></script>
<p>
By now you should see two video feeds, one inverted and one normal. You
are well on the way of creating an experimental camera! Next let's hide
the lower video feed that's automatically generated by createCapture():
</p>
<script src="https://gist.github.com/xinemata/9b6ca8e6ec2e5dc39353c4cb64e5ca2c.js"></script>
<h3>
createButton() + saveCanvas()
</h3>
<div id="p5pos">
<script src="basicCam.js"></script>
</div>
<p>↳ click on button to take a picture :)</p>
<p>
In this section, we will combine another DOM element
<a href="https://p5js.org/reference/#/p5/createButton"
>createButton()</a
>
and p5 function
<a href="https://p5js.org/reference/#/p5/saveCanvas">saveCanvas()</a> to
take a screenshot from the webcam and save it to your local device.
Let's start from adding a button to the program. And let's also create a
custom function that will execute when the button gets clicked on:
</p>
<script src="https://gist.github.com/xinemata/9a5725436ccb7abfbd2628f856763c60.js"></script>
<p>
Now whenver you click on the button, the string "you took a picture"
should be printed in your console. If you're feeling lost, it might help
to check out a different example
<a href="https://code-2.glitch.me/p3.html#input">here</a>.
</p>
<p>
The next thing we have to do is to intergrate savaCanvas() into the
program, so that everytime the button is clicked, the progam would save
a photo to the computer. The syntax for saveCanvas() is:
</p>
<h5>saveCanvas(selectedCanvas, [filename], [extension])</h5>
<p>
We want saveCanvas() to execute when the button is clicked, so we should
put it inside <i>takePic()</i>. And we can also create a variable to
store createCanvas() in:
</p>
<script src="https://gist.github.com/xinemata/11f574cca8e52c7f67ed1028d364f741.js"></script>
<p>
Now try to click on the button. If your browser downloads an image, then
hooray! You've just created a camera and took the first photo of
yourself! Your camera should work if you open the sketch on an Android
device. If you'd like your camera to work on iOS 11 devices as well,
here's an additional line of code you need to add inside your setup():
</p>
<script src="https://gist.github.com/xinemata/c0c7ae0997a3165e671ede0afbd4554c.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/G6BDhWrW"
>Link to Source Code</a
>
<p>
Note: working on camera software across different devices is a constant
struggle, because software on those devices are constantly updating.
Remember when your software stopped working after you've updated your
operating system? This is a similar issue. Just keep in mind that this
struggle is a completely normal. It's part of what it means to code.
When I encounter weird problems like this in p5, the first place I go to
find my answers is
<a href="https://github.com/processing/p5.js/issues/2847"
>p5's github issues</a
>.
</p>
<div id="clm"><h2>clmtrkr.js</h2></div>
<p>
Many of you are curious about exploring facial recognition for this
project, so I'm adding a chapter on
<a href="https://github.com/auduno/clmtrackr">clmtrkr.js</a>, a
JavaScript library that can be easily intergrated into p5.js. You can
use it to create AR face filters, analyze facial expressions, or even
<a
href="https://yp.scmp.com/article/115418/10-instagram-secret-filter-games-play-right-now"
>use your face as an input to play a game</a
>.
</p>
<p>
This might sound complicated, but the good news is the mechanism
actually works very similarly to the textToPoints() function we've
already worked with in Project #2. Instead of translating text into an
array of x and y, clmtrkr.js helps translating our facial contour into a
sequence of points —
</p>
<div class="full-width-box">
<img src="https://www.auduno.com/images/annotated2_small.jpg" />
<img
src="https://www.auduno.com/clmtrackr/docs/media/facemodel_numbering_new_small.png"
/>
</div>
<p>
What's convenient is that clmtrkr will always generate a total of 70
points around the face, so if you want to draw something on the tip of
the nose it will always be point 62, the left eye and the right eye will
always be point 27 and 32.
</p>
<p>
If you want to learn more about how clmtrkr works behind the scene, I'd
recommend checking out this
<a href="https://www.auduno.com/2014/01/05/fitting-faces/">blog post</a
>.
</p>
<h3>
Setting up clmtrkr.js in the p5 Editor
</h3>
<p>
Let's use this
<a href="https://editor.p5js.org/xinxin/sketches/EDqEwgAul"
>code template</a
>
as a starting point. It includes what we've gone through in the
createCapture() section above. To add the clmtrkr.js library into your
p5 sketch, you'd want to open your index.html page and insert the
library before your p5 library:
</p>
<script src="https://gist.github.com/xinemata/e91b73dcc33c6d24525ee1f27a7bcffa.js"></script>
<p>
Next let's open sketch.js and create a global variable
<i>let tracker</i> at the top of the sketch. And let's also add line
12~14 from the code example below into your setup(). I will give a brief
explanation of what each of those lines does next to the code, but if
you want to dig deeper please check out clmtrkr's
<a href="https://www.auduno.com/clmtrackr/docs/reference.html"
>reference page</a
>.
</p>
<script src="https://gist.github.com/xinemata/3a647090fe1d682ebde4d040b7fc3831.js"></script>
<p>
You've initated the clmtrackr object, but nothing is happening on the
screen yet. Let's add this important line in draw() so that the tracking
points will be continuously updated when the program runs.
</p>
<script src="https://gist.github.com/xinemata/f4ebf46ababda6acfb4a2483fa87da03.js"></script>
<p>
Now all the tracking points would be stored inside <i>positions</i>.
Next, let's add the block of code below into draw(). Using beginShape()
and endShape(), we can connect all the tracking points together to draw
and outline of the face!
</p>
<script src="https://gist.github.com/xinemata/2eab5fe6150eff7002aefc372e2d54a8.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/-gbq9sNsC"
>Source Code</a
>
<p>
Now keep your face in the center of frame and make sure that your face
is fairly well lit. If you see an overlayed white outline around your
face, then congrats! You've got a basic facial recognition system in
your p5 sketch.
</p>
<div class="full-width-box">
<img
style="height:200px"
src="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2FScreen%20Shot%202020-04-04%20at%204.00.14%20PM.png?v=1586030463124"
/>
</div>
<p>
Now, we've entered an uncharted territory here. What's actually inside
<i>positions</i>? What is the two-bracket business in
<i>positions[i][0]</i> and <i>positions[i][1]</i> all about? Hum, let's
print <i>positions</i> in draw() to take a peak at what's inside the
variable:
</p>
<script src="https://gist.github.com/xinemata/a1ab72f59df25c454de0c030d7cba363.js"></script>
<p>
When you hit play, you should see something like this inside your
console:
</p>
<div class="full-width-box">
<img
style="height:200px"
src="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2FScreen%20Shot%202020-04-04%20at%201.56.40%20PM.png?v=1586026141160"
/>
</div>
<p>
And if you stop the sketch and click on the tiny arrow in front of each
print, you should be able to look inside of the structure and see
something like this:
</p>
<div class="full-width-box">
<img
style="height:200px"
src="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2FScreen%20Shot%202020-04-04%20at%202.48.29%20PM.png?v=1586026141066"
/>
</div>
<p>
This is a new data structure that we haven't talked about in this class.
It's got a fancy name — the two-dimensional array.
</p>
<h3>
Two-Dimensional Array
</h3>
<p>
So far we have mostly been working with one-dimensional array. If we
compare writing an array to making a list, the list version of
<i>let groceries = [“pasta”, “dumplings”, “bread”]</i> would look like
this
</p>
<ol>
<li style="list-style-type: decimal">Pasta</li>
<li style="list-style-type: decimal">Dumpling</li>
<li style="list-style-type: decimal">Bread</li>
</ol>
<p>
However, if I really shop for those groceries in real life, I'd probably
need to create a sublist under every food item, detailing what
ingredients I need. And this is what two-dimensional array allows us to
do — we can add an additional sublist to an exisiting list.
Essentially, tw-dimensional array is an array of an array; a list of a
list.
</p>
<ol>
<li style="list-style-type: decimal; font-weight: bold;">Pasta</li>
<ol>
<li style="list-style-type: lower-alpha;">Tomato</li>
<li style="list-style-type: lower-alpha;">Basil</li>
<li style="list-style-type: lower-alpha;">Spaghetti</li>
</ol>
<li style="list-style-type: decimal; font-weight: bold;">Dumpling</li>
<ol>
<li style="list-style-type: lower-alpha;">Dumpling wrapper</li>
<li style="list-style-type: lower-alpha;">Cabbage</li>
<li style="list-style-type: lower-alpha;">Ground pork</li>
</ol>
<li style="list-style-type: decimal; font-weight: bold;">Bread</li>
<ol>
<li style="list-style-type: lower-alpha;">Flour</li>
<li style="list-style-type: lower-alpha;">Yeast</li>
</ol>
</ol>
<p>
Let's apply these concepts to p5. If I were to create a pizza ordering
progarm that lets my customers choose their own toppings using a
one-dimensional array, it would look like this:
</p>
<script src="https://gist.github.com/xinemata/0b7784cc40a1bdbadd496937b8d7538f.js"></script>
<p>
And in a two-dimensional array it would look like this:
</p>
<script src="https://gist.github.com/xinemata/d932dff09f751b807477c0636bdac340.js"></script>
<p>
I've snuck in a neat trick, <i>console.table()</i> in the code. Hit play
and see a surprise in the console :)
</p>
<p>
Most importantly, how do we order a specific pizza topping from
<i>array2D</i>? If you'd like to order a plain bread, you would type
this into the program:
</p>
<script src="https://gist.github.com/xinemata/f8ce75047435a7f47be10d9af72be242.js"></script>
<p>
Now here's a coding challenge for you — make the console print out
"I'd like a pizza with whole wheat bread, red sauce, mushrooms with
mozzarella and goat cheese". If you get stuck, check out the answer
<a href="https://editor.p5js.org/xinxin/sketches/w2axyYNwY">here</a>.
</p>
<h3>
Adding AR Overlay
</h3>
<p>
Finally, let's return to looking at the two-dimensional arrays inside
the
<a href="https://editor.p5js.org/xinxin/sketches/-gbq9sNsC"
>clmtrackr exmple</a
>. It turns out that under every tracking point, there is an [x, y]
array that store the x and y values of each tracking point. By writing
<i>positions[62][0]</i> we get to access the x value of tracking point
number 62, and by writing <i>positions[62][1]</i> we access the y value.
</p>
<p>
Knowing this, let try something fun by adding this block of code to the
clmtrackr example inside draw():
</p>
<script src="https://gist.github.com/xinemata/191bb94a848505f2ca3542557453d331.js"></script>
<p>
If you see a circle popping up in front of your nose, hooray!
</p>
<p>
You might ask why we need to add an IF statement that checks whether
<i>positions.length</i> is larger than 0. If you try to comment that out
you will get an immediate error when the program starts. The reason for
this is because it takes a second for the program to register your face.
Without the IF statement, the program would try to access the
<i>positions</i> data without actually having the data, causing the
program to crash.
</p>
<p>
For the sake of organization, the next thing we could do now is to add
dots and numbers to the sketch to show where each tracking points are on
the screen.
</p>
<script src="https://gist.github.com/xinemata/9a38b95112cdc029de067953390672e1.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/6CKO4zr56"
>Source Code</a
>
<h3>
Facial Expression Analysis
</h3>
<div class="full-width-box">
<img
style="height:500px"
src="https://static01.nyt.com/images/2018/07/18/lens/18spy10/18spy10-superJumbo.jpg?quality=90&auto=webp"
/>
</div>
<p>
↳ A detail of “Machine Readable Hito,” 2017. Trevor Paglen / Metro
Pictures, New York
</p>
<p>
While facial recognition software is mostly used as a
<a
href="https://www.vice.com/en_us/article/d3a7ym/trump-didnt-create-our-face-recognition-nightmare-hes-just-expanding-it"
>discriminatory</a
>
and
<a href="https://www.eff.org/pages/face-recognition">surveillance</a>
tool, artists and designers should feel empowered to use it in ways that
is
<a
href="https://www.nytimes.com/2018/07/18/lens/what-do-facial-recognition-technologies-mean-for-our-privacy.html"
>critical</a
>
and
<a
href="https://medium.com/@zachlieberman/m%C3%A1s-que-la-cara-overview-48331a0202c0"
>expressive</a
>. It's our responsibility to work with a problematic tool with
thoughtfulness and intentionality.
</p>
<p>
Here's an example of how you would analyze someone's smile — add
this block of code into your existing clmtrakr sketch:
</p>
<script src="https://gist.github.com/xinemata/dc4567a2d54bddb786df6b6f8e454b34.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/FQRpSS-23"
>Source Code</a
>
<p>
If you try out the sketch, you'd see that this idea of "emotional
analysis" is actually extremely superficial. Essentially, you are just
comparing the distance and relationship between two or more tracking
points on the face. Try adding your own feature, such as making the bar
grow if your eyebrow raises. Once you get comfortable with adding your
own features, try to find creative and surprising ways to work with the
technique.
</p>
<div id="cg"><h2>createGraphics()</h2></div>
<p>
Remember when you first learned about the background() function in p5,
and you had to choose between leaving a trail of ellipses on the screen
when you moved your mouse around versus clearing the background and
leaving only one ellipse on the screen? Well, those days are officially
over —
<a href="https://p5js.org/reference/#/p5/createGraphics"
>createGraphics()</a
>
give you the ability create multiple layers on top of your canvas. You
could do things such as assigning a background() to the bottom layer and
no background for the top layer; or cut part of a layer and paste it
onto a new layer. This is pretty much the closest to Photoshop we're
ever going to get in p5.js!
</p>
<div class="full-width-box">
<img style="height:100px" src="https://i.imgur.com/34MoKlQ.png" />
</div>
<p>
Just like createCanvas(), createGraphics() is a
<a href="https://p5js.org/reference/#/p5.Element">p5.elements</a> that
is usually initiated in setup():
</p>
<script src="https://gist.github.com/xinemata/f0e9d26655c32fb9bd7299b402abb4a2.js"></script>
<p>
The line <i>layer1.ellipse(mouseX, mouseY, 25, 25)</i> assigns
<i>ellipse()</i> as a child of <i>layer1</i>. There are many other p5
functions that could be assigned as children of the layers, check out
the source code for the sketch below to see a more complete version of
my drawing program —
</p>
<iframe src="https://editor.p5js.org/xinxin/embed/7RQIl3W7S"></iframe>
<p>↳ hold your mouse down to draw</p>
<a href="https://editor.p5js.org/xinxin/sketches/7RQIl3W7S"
>Source Code</a
>
<p>
Now, if we combine createGraphics() with createCapture(), we could come
up with many wonderful ideas for our experimental camera. For instance,
I could
<a href="https://editor.p5js.org/xinxin/sketches/qouzSxCGK"
>draw a picture with my nose</a
>!
</p>
<div id="copy"><h2>copy()</h2></div>
<p>
<a href="https://p5js.org/reference/#/p5/copy">copy()</a> is a p5
function that lets you copy and paste a specific region of the canvas.
Like the good ol' days of Photoshop. This is the syntax for copy():
</p>
<h5>
copy(srcImage, sx, sy, sw, sh, dx, dy, dw, dh)
</h5>
<p>
As you can see, there are nine parameters inside the copy function! This
may seem daunting at first, but once you understand what each of them do
it is actually pretty straight forward. Let's break them down one by
one:
</p>
<ul>
<li>
(1) srcImage: source image. This can be an image you upload, or an
image generated by createImage(), createGraphics(), or createCapture()
</li>
<li>(2) sx: source image's x coordinate</li>
<li>(3) sy: source image's y coordinate</li>
<li>(4) sw: source image width</li>
<li>(5) sh: source image height</li>
<li>(6) dx: destination image's x coordinate</li>
<li>(7) dy: destination image's y coordinate</li>
<li>(8) dw: destination image width</li>
<li>(9) dh: destination image height</li>
</ul>
<p>
Now let's put it into practice. Download this
<a
href="https://cdn.glitch.com/5dd99b18-3ebb-45c5-90fb-b4b67dc2e128%2F1_7MmkjYFuzatsK66uNfGGzQ.jpg?v=1586270002891"
>seaside photo</a
>
and upload it to your p5 sketch. Here's a
<a href="https://p5js.org/reference/#/p5/image">reminder</a> for
displaying an uploaded image in your editor.
</p>
<p>
The goal we have here is to copy the goats from the original image and
paste it to a different location on the canvas. In the example below, I
have copied two goats for you. Could you try to copy a different goat?
</p>
<div class="full-width-box">
<iframe src="https://editor.p5js.org/xinxin/embed/Bc7HB0VKK"></iframe>
</div>
<div class="full-width-box">
<script src="https://gist.github.com/xinemata/a9f6a60d8fbd5c5cd1353eeef4e7157d.js"></script>
</div>
<a href="https://editor.p5js.org/xinxin/sketches/Bc7HB0VKK"
>Source Code</a
>
<p>
Once you've done it yourself, you might notice that even though our
canvas is at 400px by 400px, we might need to set our source X and
source Y above 400. Do you have an idea for why that may be?
</p>
<p>
Tips: you could use
<a href="https://p5js.org/reference/#/p5/imageMode">imageMode()</a> to
let the progam know whether to treat the CENTER or the UPPER LEFT of the
image as (0,0).
</p>
<h3>
Make a Pixelator
</h3>
<iframe
width="560"
height="315"
src="https://www.youtube-nocookie.com/embed/ENf9MXDj4hs"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
<p>
Now that we know how copy() works, let's combine it with createCapture()
and make a real-time pixelator!
</p>
<p>
Pixelation happens when a raster image is enlarged to the point where
individual pixels are revealed, resulting in blurriness and fuzziness in
the image. Based on this principle, we can stretch and displace regions
of an image to create a pixelated effect. We will use a nested for loop
to copy a grid from the source image and paste the grid onto the canvas.
</p>
<script src="https://gist.github.com/xinemata/24a0c19a36382162754670473cf99314.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/62FPCIYeN"
>Source Code</a
>
<p>
Once you feel comfortable with the pixelator tutorial, check out this
<a href="https://editor.p5js.org/xinxin/sketches/8HR7jS5Y">example</a>
that creates a swaying effect by combining the technique with a sine
wave.
</p>
<div id="setget">
<h2>
set() and get()
</h2>
</div>
<p>
<a href="https://p5js.org/reference/#/p5/set">set()</a> and
<a href="https://p5js.org/reference/#/p5/get">get()</a> are easy-to-use
functions that let you directly interact with the pixels on your canvas.
set() lets you write pixels to the canvas and get() lets you read
pixels() from the canvas.
</p>
<h5>
set(x, y, c)
</h5>
<p>
In order to use set(), you would need to specify which x, y positions
you would like to place the pixel in, and what color you would like the
pixel to use.
</p>
<h5>
get(x, y)
</h5>
<p>
In order to use get() you first need to make sure that the canvas isn't
empty. Specify the x and y of the pixel you would like to read, and a
color value will return which you could use for something else in your
sketch.
</p>
<h3>
What's Inside a Pixel?
</h3>
<div class="full-width-box">
<img
src="https://upload.wikimedia.org/wikipedia/commons/4/4d/Pixel_geometry_01_Pengo.jpg"
/>
</div>
<p>
A <a href="https://www.youtube.com/watch?v=m8c1CAT2zEI">pixel</a> is the
smallest element in a digital image. Every pixel on your screen contains
an array of [R, G, B, A]. R stands for red, G stands for green, B stands
for blue, and A stands for the alpha channel, which controls opacity.
Let's write a few lines of code so that we could read the array of a
pixel in our console:
</p>
<script src="https://gist.github.com/xinemata/2efbfb6b2861ea531de832458155b0dc.js"></script>
<p>
If you print <i>c</i> into the console, you would see an array of four
different values, each representing the R, G, B, and Alpha channel of
the single pixel that is right underneath the tip of the cursor. Since
we've set the background to green, getting the pixel at any part of the
canvas is going to give us a reading of <i>[0, 255, 0, 255]</i>.
</p>
<p>
Let's move forward to a more nuanced example using the seaside photo as
the background. I'm using set() to display the image in draw(). And I'm
also using <i>mouseX</i> and <i>mouseY</i> as the x and y values in
get(), so that when you hover over the screen, you would see the color
of the pixel you're hovering over displayed inside the square —
</p>
<iframe src="https://editor.p5js.org/xinxin/embed/RkoemCNwi"></iframe>
<p>↳ hover over the screen to pick a color from the image</p>
<script src="https://gist.github.com/xinemata/f843023cae788c95bc4eb71e1740089d.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/RkoemCNwi"
>Source Code</a
>
<p>
Here's another example that uses both set() and get() in a clever way
—
</p>
<iframe src="https://editor.p5js.org/xinxin/embed/VemrYWnSS"></iframe>
<p>
As you can see, I've placed the get inside set(), so that everytime
set() is executed, it loads get() which finds anoter pixel at a relative
position to grab.
</p>
<script src="https://gist.github.com/xinemata/17a19a9d64e888255066a029fc55a5d1.js"></script>
<a href="https://editor.p5js.org/xinxin/sketches/VemrYWnSS"
>Source Code</a
>
<h3>
Make a Pointillist Filter
</h3>
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/aza-3oZjY4g"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
<p>