<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/face-recognition-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.stoltzmaniac.com/facial-recognition-in-r/"> R-Projects – Stoltzmaniac</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3>Face Recognition in R</h3>
<p>OpenCV is an incredibly powerful tool to have in your toolbox. I have had a lot of success using it in Python but very little success in R. I haven’t done too much other than searching Google but it seems as if “imager” and “videoplayR” provide a lot of the functionality but not all of it.</p>
<p>I have never actually called Python functions from R before. Initially, I tried the “rPython” library – that has a lot of advantages, but was completely unnecessary for me so system() worked absolutely fine. While this example is extremely simple, it should help to illustrate how easy it is to utilize the power of Python from within R. I need to give credit to Harrison Kinsley for all of his efforts and work at  <a rel="nofollow" href="https://www.pythonprogramming.net/" target="_blank">PythonProgramming.net</a>  – I used a lot of his code and ideas for this post (especially the Python portion).</p>
<p><span></span></p>
<p>Using videoplayR I created a function which would take a picture with my webcam and save it as “originalWebcamShot.png”</p>
<p><strong>Note:</strong> saving images and then loading them isn’t very efficient but works in this case and is extremely easy to implement. It saves us from passing variables, functions, objects, and/or methods between R and Python in this case.</p>
<p><img data-orig-file="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/06/modifiedWebcamShot-smaller-min.png?fit=900%2C506&amp;ssl=1" data-orig-size="900,506" data-medium-file="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/06/modifiedWebcamShot-smaller-min.png?fit=300%2C169&amp;ssl=1" data-attachment-id="466" data-image-meta="{" data-image-description="" src="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/06/modifiedWebcamShot-smaller-min.png?resize=557%2C314" data-permalink="https://www.stoltzmaniac.com/facial-recognition-in-r/modifiedwebcamshot-smaller-min/" data-large-file="https://i2.wp.com/www.stoltzmaniac.com/wp-content/uploads/2017/06/modifiedWebcamShot-smaller-min.png?fit=620%2C349&amp;ssl=1" alt="" data-image-title="modifiedWebcamShot-smaller-min" sizes="(max-width: 557px) 100vw, 557px" data-comments-opened="0" class="wp-image-466" data-recalc-dims="1"></p>
<p>I’ll trace my steps backward through this post (I think it’s easier to understand what’s going on in this case).</p>
<h4>The main.R file:</h4>
<ol>
<li>Calls my user-defined function
<ul>
<li>Turns on the camera</li>
<li>Takes a picture</li>
<li>Saves it as “originalWebcamShot.png”</li>
</ul>
</li>
<li>Runs the Python script
<ul>
<li>Loads the previously saved image</li>
<li>Loads the Haar Cascade algorithms</li>
<li>Detects faces and eyes</li>
<li>Draws colored rectangles around them</li>
<li>Saves the new image as “modifiedWebcamShot.png”</li>
</ul>
</li>
<li>Reads new image into R</li>
<li>Displays both images</li>
</ol>
<pre>source('imageFunctions.R')
library(&quot;videoplayR&quot;)

# Take a picture and save it
img = webcamImage(rollFrames = 10, 
                  showImage = FALSE,
                  saveImageToWD = 'originalWebcamShot.png')

# Run Python script to detect faces, draw rectangles, return new image
system('python3 facialRecognition.py')

# Read in new image
img.face = readImg(&quot;modifiedWebcamShot.png&quot;)

# Display images
imshow(img)
imshow(img.face)</pre>
<p>The user-defined function:</p>
<ol>
<li>Function inputs
<ul>
<li>rollFrames is the number of pictures to take (allows the camera to adjust)</li>
<li>showImage gives the option to display the image</li>
<li>saveImageToWD saves the image generated to the current working directory</li>
</ul>
</li>
<li>Turns the webcam on</li>
<li>Takes pictures (number of rollFrames)</li>
<li>Uses basic logic to determine to show images and/or save them</li>
<li>Returns the image</li>
</ol>
<pre>library(&quot;videoplayR&quot;)

webcamImage = function(rollFrames = 4, showImage = FALSE, saveImageToWD = NA){

  # rollFrames runs through multiple pictures - allows camera to adjust
  # showImage allows opportunity to display image within function

  # Turn on webcam
  stream = readStream(0)

  # Take pictures
  print(&quot;Video stream initiated.&quot;)
  for(i in seq(rollFrames)){
    img = nextFrame(stream)
  }

  # Turn off camera
  release(stream)

  # Display image if requested
  if(showImage == TRUE){
    imshow(img)
  }

  if(!is.na(saveImageToWD)){
    fileName = paste(getwd(),&quot;/&quot;,saveImageToWD,sep='')
    print(paste(&quot;Saving Image To: &quot;,fileName, sep=''))
    writeImg(fileName,img)
  }

  return(img)

}</pre>
<p>The Python script:</p>
<ol>
<li>Loads the algorithms from xml files</li>
<li>Loads the image from “originalWebcamShot.png”</li>
<li>Converts the image to grayscale</li>
<li>Runs the facial detection algorithm</li>
<li>Runs the eye detection algorithm (within the face)</li>
<li>Draws rectangles around the face and eyes (different colors)</li>
<li>Saves the new image as “modifiedWebcamShot.png”</li>
</ol>
<pre>import numpy as np
import cv2

def main():

  # I followed Harrison Kingsley's work for this
  # Much of the source code is found https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/

    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

    img = cv2.imread('originalWebcamShot.png')

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imwrite('modifiedWebcamShot.png',img)

if __name__ == '__main__':
    main()</pre>
<p>The Python code was entirely based off of Harrison Kinsley’s work:</p>
<ul>
<li>@sentdex <a rel="nofollow" href="https://twitter.com/Sentdex" target="_blank">Twitter</a> | <a rel="nofollow" href="https://www.youtube.com/sentdex" target="_blank">YouTube</a></li>
<li>Website: <a rel="nofollow" href="https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/" target="_blank">PythonProgramming.net</a></li>
</ul>
<p>Thanks for reading. As always, the code is on my <a rel="nofollow" href="https://github.com/stoltzmaniac/ML-Image-Processing-R" target="_blank">GitHub</a></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.stoltzmaniac.com/facial-recognition-in-r/"> R-Projects – Stoltzmaniac</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ZGjjS7S_8bE" height="1"></a></div>