Wednesday, June 16, 2021

Digital Advertisement Basic Understanding


Digital Advertisement Basics And Understanding


  • Understanding Mobile Ads & Advertisement Terminology.
  • Understanding how SSP and DSP works.
  • Understanding how Ad Exchange & Ad Network works.
  • Understanding how Ad Exchange & Ad Network works & Mediation works.
  • What are major companies in Advertisement?
  • Where to find Ad Network and Global Advertisement company?
  • How to check for Ad Company category, whether SSP or DSP?
  • How to optimise yield?
  • How Digital Ad Network works?
  • How Mobile Ad Network Works?
  • How Web Ad Network Works?
  • What are major challenges with Mobile Advertisement?
  • What are major challenges with Mobile Ad Companies?
  • Mobile Ad Puzzels to be solved?

Understanding Mobile Ads & Advertisement Terminology.

In Mobile advertisement couple of terms which you hear most when working on mobile ad tech team or in a advertisement.

Ad Exchange
An ad exchange is a technology platform that functions primarily as a digital marketplace. It allows advertisers and publishers to buy and sell online ad space from various ad networks through real-time auctions. The payoff is efficiency and transparency. Ad exchanges enable advertisers to gauge prices for ad impressions across multiple sites and purchase those that are most cost-effective. All of this is done simultaneously, eliminating any lengthy ad-buying negotiations.

Ad Network
Ad networks are companies that gather ad space supply from publishers and sell it to advertisers, typically at a marked up price. In short, they act as middlemen who connect companies that create ads with websites with the space to display those ads.
Ad Server
An ad server is a web server that publishers use to store, manage, and deliver their ads to website visitors. Ad servers often employ advanced analytical tools, allowing publishers to use data to better understand and optimize their advertising model. For example, ad servers can count and track users, generating data-fueled reports for advertisers on the number of impressions their ads receive.

Ad Tag
An ad tag is code publishers place on websites in order to sell ad space. It consists of two parts: 1) a URL and 2) a piece of HTML or JavaScript code. Working together, these two parts first request content (ads or other ad tags) from the URL and then instruct the browser how to display the content.

API
An application programming interface (API) is a language format, written in code, which allows programs and applications to communicate with each other and their respective operating systems. The language creates a standard of rules and protocols which programmers use to develop software that doesn’t conflict. In the mobile ad tech sector, API-powered mobile devices offer greater visibility into a user’s lifestyle, delivering data that can create marketing opportunities and inform strategic decisions.

App

An app is simply an application. The term is specifically used to differentiate an application on a mobile device, such as a mobile game, from programs on a traditional desktop computer.
Attribution
Attribution is the process by which user interactions are identified and measured. It’s a way in which marketers garner a better understanding of how certain events lead users to a desired outcome, referred to as a conversion. Attribution quantifies an ad’s ability to influence a consumer’s purchasing decisions, providing marketers with a way to compare the effectiveness of various marketing campaigns.

Banner
A banner is any type of advertisement that acts as a “banner” displayed usually at the top or bottom or the webpage or app. Banner ads are still very popular today, and feature both text and graphics.

Creative
Creative, specifically ad creative, is a file that houses the digitally formatted design and artwork for an advertisement. This file is rendered as a display ad on the publisher’s medium and can take the following formats: Image (GIF, PNG, JPEG), Flash File (SWF), HTML or JavaScript.

CPA
Cost-per-Action (sometimes known as Pay Per Action or PPA; also Cost Per Conversion) is an online and mobile advertising pricing model, where the advertiser pays for each specified action. For example, an action after an initial impression and click, like an install, form submit (e.g., contact request, newsletter sign up, registration etc.), double opt-in or in-app sale. Formula: CPA = Cost/Number of Actions

CPC
Cost-per-Click (CPC) is the price the advertiser pays a publisher every time a consumer clicks on the ad. The price is set by the advertiser. Formula: CPC = Cost/Number of Clicks

CPI
Specific to mobile applications, Cost-per-Install (CPI) is the price an advertiser pays whenever the consumer installs the advertised application. Formula: CPI = Cost/Number of Installs

CPM
Cost-per-Mille (CPM) is a pay structure designed to generate brand awareness. The advertiser pays the publisher for every 1000 times the advertisement is displayed to a consumer. Formula: CPM = Cost X 1000/Impressions

CTR
Click-through rate (CTR) is the ratio of clicks to ad impressions. This is the most commonly used metric to determine the success of an ad campaign.Formula: CTR = Number of Clicks/Impressions

DMP
A Data Management Platform (DMP) is a centralized digital warehouse where marketers, publishers, and other businesses can effectively store, manage and analyze large quantities of data.  DMPs are incredibly useful in marketing campaigns, enabling optimization through more effective ad targeting. AudienceArchitect™ is, of course, our mobile DMP of choice.

DNT
Do Not Track (DNT) is specifically a HTTP header field that sends a signal to other websites, namely analytics companies, ad networks and social platforms, requesting them to disable any tracking of individual users. Despite the request, many sites still do not honor the DNT signal. There currently exists no standardized protocol for its enforcement.

DSP
A Demand Side Platform (DSP) is a centralized technology platform that enables automated ad buying from a range of publisher sites while simultaneously connecting with consumers through vertical and lateral targeting. It’s an all-in-one tool for advertisers, efficiently integrating the buying, delivering and tracking of ads through proper utilization of data. The result is campaign optimization. Marketers can manage and tailor both their bids and their data to more effectively reach their targeted audiences. There are a handful of good mobile DSPs out there. Ours is AdCast™.

eCPM
Effective Cost-per-Mille (eCPM) is a way to measure the value of a publisher’s inventory on a Cost-per-Mille (CPM) basis. eCPM is calculated by multiplying the number of clicks (CTR) by the CPC rate to  determine total revenue. That total is then divided by the number of blocks of 1,000 impressions delivered, giving the eCPM value. Formula: eCPM = (Total Spent/Impressions Delivered)X 1000

Fill Rate
The fill rate is the rate at which a publisher successfully displays an ad in relation to the number of times the ad was requested. Essentially, this rate evaluates the amount of wasted inventory space a publisher has.

GRP
Gross Rating Point (GRP) is a standard measure for the impact or exposure of an ad campaign. GRPs calculate reach multiplied by exposure frequency. For example, if an ad is exposed to 32% of a targeted audience and that exposure occurs a total of three times at the same 32% rate, then you have a GRP of 96. Because the GRP measures gross, it is therefore possible to have a number over 100.

Impression
An ad impression is the calculated instance of an ad being displayed to a human consumer. Impressions give marketers a broad understanding of how many people their brand is reaching.

Inventory
Ad inventory is the total amount of space a publisher has on their respective platform to display advertisements from advertisers. Ad inventory is sold to an advertiser at a price, often determined through an online bidding system. Specifically mobile ad inventory is often measured in impressions.

Mediation
Mobile ad mediation is a technology that allows publishers to maximize the revenue gained from selling impressions. The ad mediation platform ranks ad networks according to publisher priorities, enabling the publisher to quickly find and choose ad networks that provide the highest potential revenue for their inventory.

Programmatic Buying
Programmatic buying is automated ad buying. It allows advertisers and publishers to quickly buy and sell advertisements through computerized systems and without the need for human intervention.

ROI
Return on Investment (ROI) is a metric used to determine the benefits of an investment and compare that to the cost.

RTB
Real-time bidding (RTB) is the process by which ad inventory is bought and sold instantaneously through programmatic means. The auctions, which take place every time a web browser opens a website, sell ad space provided by publishers to the advertiser willing to pay the highest price. The entire process is facilitated by ad exchanges.

Rich Media
Rich media constitutes a kind of ad that will typically contain some form of video or user interaction engagement. Rich media allows advertisers to connect with and involve consumers on a deeper level, providing dynamic content and effects.

SDK
A software development kit (SDK) is a set of programming tools for developers and programmers to use for the creation of a wide range of applications for various software packages. In mobile tech, these tools are often made available to customers, offering an intuitive, easy-to-use programming kit to develop their own mobile apps. Once created, apps created from publishers and consumers alike can be published and sold over the popular app marketplaces.

SSP
A supply side platform (SSP) is a technology that allows publishers to maximise the revenue gained from selling their ad inventory. It’s an automated system that connects publishers to multiple ad networks and exchanges to facilitate the purchase of inventory. Publishers then can receive the best possible price as their inventory is exposed to the highest number of potential bidders possible.

Viewability
Was your ad seen by a human? If so, for how long? These are the tough questions that mobile view-ability seeks to answer for advertisers and publishers alike. For a comprehensive explanation on mobile viewability, check out our dedicated resource. 

Understanding how SSP and DSP works.

First basic thing to remember is SSP & DSP are kind of same technology, which are designed for the benefit for their customer. what SSP does in supply side same DSP does in Demand side, How?
SSP: 




Some Important link:



Monday, October 24, 2016


Advertisement Vocabulary:


I am listing down some important and good to remember advertisement terminology.

Advertisers: Marketers from brands and business who buy advertising. Can be broken down into two main types. The first are called “Brand or Institutional” advertisers and the second “Direct Response” advertisers. Brand advertisers focus promoting a brand’s image or a particular initiative. Direct response advertisers focus on driving app downloads, generating sales and generating leads. 

Advertising Option Icon: The “i” triangle button is placed on ad creatives so a consumers can learn how their data is collected and used. Administered the AdChoices, an industry consortium dedicated to self regulation. 

Ad Blockers: Software enabled through a person’s web browser or mobile device which can prevent ads from being displayed.

Ad Exchanges: A marketplace that automates the ad buying process for both the advertiser (buyer) and the publishers (seller). There are 
many different types of exchanges and they operate under various models.

Ad Integration: The technical process of defining and making available ad slots within a publisher’s site or app. 

Ad Network: An Ad Network is a company that connects advertisers to publishers. The network model aggregates many different publishers into one generic ad buy, usually at steep discount. Buyers have very little control over where the ads are displayed. Some networks are totally blinded, they offer no insight into what sites and apps the ads appear on. While others are semi-blinded and provide the advertiser with a list of publisher that the ads will appear on. 

Ad Ops (Online Advertising Operations): The team and process which aid in the sales and delivery of online advertising. Specifically the process and systems that sell, input, serve, target and report on digital ad campaigns. 

Ad Request: The request made to an ad server to display an advertisement. The request doesn’t always result in an ad displayed for a variety of reasons, including mobile network latency. 

Ad Servers: The technology that disseminates a digital ad and measure the performance of that ad. 

Ad Slot: Refers to a space in an app that has been reserved for advertisement. 

Ad Unit: Any predefined advertising vehicle that can appear in an app or on a mobile website. For example, a rectangular banner is considered to be a common type of ad unit. 

Ad Verification: Tools that advertisers use to determine if impressions are displayed, where they are displayed and if they meet the advertiser’s privacy criteria. It’s popular to use an ad verification system to verify blinded buys and provide confidence that the proper audience has been reached. 

Agencies: Advertising agencies are businesses that are dedicated to creating, planning, and handling advertising for their clients. Many advertising agencies specialise in a specific area such as social media, online advertising, mobile advertising, and search engine marketing. 

Agency Trading Desks (ATDs): A media buying platforms owned and operated by an agency.

Algorithm: This is a step by step procedure for calculations. Algorithms are used for calculations, data processing, and automation. 
Algorithms are what powers the RTB and DSP space. 

Analytics:
  • Third Party: Generally a survey based approach popular in traditional media. Examples include companies 
  • First Party: Software tools (Google Analytics, Omniture, Webtrends among them) operated through tags on a site’s pages or in an app, which provide data on usage, sales conversion, clicks, impression and other digital metrics. 
API (Application Programming Interface): A predetermined way for a software components to interact with each other. API’s are commonly included in SDKs. 

Audience Targeting: The practice of using data to imply an audience, either by demography, life stage or intent. One example is targeting people who have searched for information on a cars or mobile phones. 

Beacons: A 1x1 pixel tag typically used by an advertiser or a third party ad server to track a unique user’s activity over times. 

Behavioural Targeting: What is now generally referred to as “audience targeting.” It’s the process of looking at behaviour instead of context as a method for ad targeting.

Big Data: A term used to describe a data driven approach to business. The Big Data approach stresses the use of data and quantitative analysis as the primary factor in business decision making. 

Blocked Sites: Publishers who an advertisers or ad buying platform have deemed inappropriate to appear on that site. Generally referring to a publisher that was available on a buying platform, but was blocked for violating the terms of service of the buyers. 

Brand Impact Studies: Studies which use control/exposed methodology to determine whether an ad that was served had an impact on metrics like ad recall, brand favourability and purchase intent. 

Call to Action: A statement or instruction, that explains to a user how to respond to an opt-in for a particular promotion or mobile initiative, which is typically followed by a notice. 

Click-to-Call: A mobile specific ad interaction type where a user clicks an ad which launches a phone call. Popular with paid search, direct response and small local businesses. 

Click Through Rate: A way of measuring the success of a digital advertising campaign. CTR is obtained by dividing the number of users who clicked on an ad by the number of impressions. 

Contextual Targeting: A contextual advertising system scans the text of a web for certain keywords and returns advertisements to the webpage based on ad messages that match with those keyword. Example: ads on the side of your Gmail that pop up text ads based on your email content. 

Natural Contextual Targeting: The traditional method of ad buying which looked at the contextual nature of the editorial content and placed relevant ads into it. An example would be a car advertisement running in a car review magazine. 

Conversion Tracking: Conversion tracking allows marketers to measure app installs driven from advertising and marketing. The technology is used primarily in cookie-less environments like in-app ads. 
Cookies:
  • First party cookies: Used by an individual publisher to identify a visitor in an anonymised way. 
  • Third party cookies: Used by an ad-server to identify and categorise a visitor into a targeting segment or classification.
  • Cookie Deletion: The process of deleting cookies from your web browser to avoid detection by advertising technology.
  • Cookie-less Technology: Any substitute to cookie based identification. Used in mobile app advertising to target and classify users generally through IFA (Identification for advertisers) from Apple. 
CPC (Cost per Click): A digital advertising business model where an advertiser only pays for ads clicked upon, popularised by Google when they introduced Adwords. 

CPI (Cost per Install): The cost required for a marketer to generate an app install. Typically this is a fixed price charged by a DSP to a marketer regardless of the CPM or CPC price. 

CPM (Cost per Mil): The Cost per Mil (thousand impressions delivered) is a method of ad buying where a given publishers charges on this basis of ad exposure. Poplar with brand advertising where click throughs are not relevant. 

eCPM: A mathematical process of calculating a publisher CPM from a different methods such as CPC. Generally performed by publishers who have buyers using both CPM and CPC. By calculating an eCPM they can better manage yield optimisation. The eCPM calculation is: cost/(impressions delivered/1000). 

Creative Optimisation: The process of favouring the highest performing creative by adjusting the frequency of creative exposure, typically on a direct response basis. 

DAA (Digital Advertising Alliance): A trade organisation that promotes industry self regulation including: AAAAs, AAF, ANA, DMA with support from the CBBB. The alliance includes over 5,000 member companies.

Data Aggregators: Platforms that aggregate ad serving data, conversion data and other 3rd party data sources including offline data. 

Data Leakage: The practice of scraping or matching user attributes from targeting data in an unintended way. Generally, but not always a violation of a platform’s or publisher’s terms of service. 

Device Type: A class of devices with identical capabilities. All devices of the same device type can be treated interchangeably. For example: iPhone 5 or Samsung Galaxy. 

DDM (Data Driven Marketing): An approach to marketing that stresses the use of quantitative analysis to determine the targeting, timing, and content of marketing promotions. 

Digital Fingerprinting: App tracking methodology that collects parameters about a specific mobile user such as the time stamp or network visited. This is used to track a mobile user’s activity in a completely anonymised fashion.

Direct Sold: Inventory sold by a publisher directly through their sales team. 

DMA (Designated Market Area): In the US a DMA represents counties, zip codes or sometimes split counties that contain a specific population that can be targeted by advertising. 

DMPs (Data Management Platforms): A platform used to collect unstructured audience data from analytical tools, CRM, point of sale data, social, advertising or any other available sources. 

DSP (Demand Side Platform): A platform that automates media buying across multiple sources. It generally provides the buyer with uni ed targeting, data, optimisation and reporting. 

Dynamic Pricing: The practice of publishers dynamically changing the price of their inventory in step with market demand.

Engagement Rates: Percent of total impressions that were clicked, liked, or commented on regardless of platform. 

Exchange: Used to define a business model where vendors place themselves as a middleware solution for the direct purpose of driving market liquidity between the demand side and the supply side. 

Fill Rate: The percentage of ad requests that are filled with ads.

Frequency Capping: The practice of managing the number of times a user views a specific ad creative. 

HTML5: The 5th revision of HTML code. Includes support for animation, video and other rich media functionality. Used exclusively by Apple as a replacement to Flash for delivery of rich media content. 

IDFA (Identifier For Advertisers): Tracking method used by advertisers for devices running Apple iOS 6 or higher. Allows for user opt out at the device level. This supplanted the use of UDID (Unique Device Identifier) prior to iOS 6. 

Impression: Measurement unit for digital advertising, where a single view of an ad is counted as one impression. Interstitial Ads: A full screen ad slot that appears between the current user state and next within an mobile app or game. 

Inventory: The estimated total amount of ad space a publisher has available to sell to an advertiser.

Premium Inventory: Ad impression which is in high demand and gives the advertiser the highest possible ROI. 

Remnant Inventory: Ad impression from low performing placements that publishers offer up to be resold as part of blinded or semi- blinded package. 

Latency: The length of time between, a user taking an action (such as clicking) and the response of the application to perform the task. This is generally used to refer to the length of time between a page loading and the ads appearing on that page. 

LUMA Landscape: A map of the marketing technology space which shows different technologies and where they sit in the ecosystem. It’s created by an investment banking rm called LUMA Partners. 

Media Silos: A term used to describe the specialisation of media buying and planning that happens in a large agency. Teams are generally based on a specific media type such as TV, outdoor, digital or social and don’t have domain knowledge of expertise in an area outside to their own. 

MMA (Mobile Marketing Association): The Mobile Marketing Association is the premier global non-pro t trade association established to lead the growth of mobile marketing and its associated technologies. 

Native Advertising: An ad unit that looks like a piece of content from a specific publisher. It generally is labeled as “sponsored content or post.” Publishers such as Facebook, Twitter, LinkedIn and Buzz-feed already incorporate native ad formats into their published content. 

OPEN RTB: A project started in November 2010 by the IAB to develop an API specification for companies interested in an open protocol for the automated trading of digital media across a broader range of platforms, devices, and advertising solutions. 

Pre-roll: The streaming of a mobile advertising clip prior to a mobile TV/video clip. The mobile ad is usually 10-15 seconds in length. 

Private Marketplaces And Exchanges: The ability of a publisher through an SSP to carve out a portion of their inventory and sell it programmatically. Generally set up the following three ways: 
  • Tiered Auctions: The publisher sells their inventory on a RTB exchange, however they create a priority “tier” to allow only certain advertisers access to the inventory ahead of other buyers. 
  • Segmented Auctions: The publisher white labels an RTB platform and sells their inventory separately from that RTB platform. 
  • Exclusive Access: The publisher allows buyers and sellers to negotiate predefined terms for pricing, availability of inventory, transparency, etc. RTB becomes the delivery vehicle for these terms. 


Programmatic Buying: The practice of automating the digital media buying process. 

Programmatic Selling: The practice of using a DSP to automate the sale of media assets. 

Programmatic Trading: The practice of fully automating both the buying and selling of online media with little or no human intervention. 

Publishers: Companies that produce the content on websites or apps where ads are placed.

Push Messaging: Any content sent by advertisers and marketers to your mobile device at a time other than when you requested it. Push 
messaging can include audio, SMS, e-mail, multimedia messaging, cell broadcast etc. 

Retargeting: A form of online advertising by which ads are targeted to consumers based on their previous internet activity. There are various types of retargeting including: 
  • Site: Targeting users who visit a page of your website. 
  • Email: Targeting users who open your emails. 
  • Dynamic: Targeting users with ads of the specific items or products they were viewing. 
  • Search: Targeting users who search for specific search queries in search engines. 
  • CRM: Targeting offline users by on-boarding offline data such as a mailing address to a data provider and matching up IP addresses. 

ROI (Return On Investment): A metric that describes how much money is gained or lost on marketing relative to the
amount invested.

RTB (Real Time Bidding): Method of selling and buying online display advertising in real time one ad impression at a time. This happens instantaneously through a live auction, very similar to the automated stock trading on a stock market exchange. 

Second Price Auction Model: When an ad sale through fails to hit the floor or minimum pricing set and the publisher decides it’s worth putting it out again at a different price. 

Semantic Targeting: A more advanced version of contextual targeting. A technology that actually reads the text of a page and targets ad based on it’s ability to read the text, rather than just match individual keywords. 

Sentiment Analysis: Used to determine if the “sentiment” of the page is positive or negative.

Social mvv based on data from social networks and an individuals “social graph” or network of
friends.  

Stack: Used to describe the different technology layers in place to facilitate the buying or selling of digital advertising. Different vendors specialise on different parts of the “stack” and the pieces all operate together to form an ecosystem. 

Supply Side Platforms (SSP): A platform that automates publisher media optimisation process from multiple sources and provides uni ed reporting. 

Transparency: Generally refers to advertisers requesting a complete lists of sites and apps they can advertise on through either a network or an exchange. 

UDID (Unique Device Identifier): Hardware-based identifier unique to Apple’s iOS which was used by advertising companies to gather user data to improve ad relevance. Replaced in 2012 with IFA (Identifier for Advertisers). 

Unified Optimisation: The process of unifying tools and reporting available through an SSP so that a publisher can look at their inventory in a holistic manner and optimise their sales accordingly. 

Unique User: Refers to the number of distinct individuals requesting ads during a given period.

View Through: The practice of tagging a user that didn’t click, but did view an ad. If a conversion action is take such as a sale that is 
referred to as a View Through Conversion. 

Viewable Ads: A classification of ads placements that are actually seen by a users rather than just loaded on a page or into an app. 

VCs: Venture capitalists are a specialised segment of the financial industry who focus on early-stage, high-potential, high risk, growth companies. 


Yield Optimisation: The process a publisher goes through in order to optimise their demand partners and maximise ROI from the inventory being sold. Generally publishers give priority access to the demand partner paying the highest CPM and tier the partners access by CPM value. 

Wednesday, August 27, 2014

iOS Face and Facial-Part Detection

iOS Face and Facial-Part Detection
With Sample Live App





Steps for iOS face and facial part recognition:

1: Setup AVConnection
  1. create a new AVCaptureSession
  2. Set SessionPreset for adjusting video and picture quality
  3. create instance of AVCaptureDevice with media type video
  4. create instance of AVCaptureDeviceInput with device instance created above
  5. check whether current deviceInput can add input to session or not and if yes
  6. addInput to session using device input.
  7. create AVCaptureStillImageOutput instance and AVCaptureVideoDataOutput instance one by one.
  8. For StillImageOutput add observer for “capturingStillImage” key with observing option new
  9. then check whether current session can add still image output or not and if yes
  10. addOutput to instance of AVCaptureStillImageOutput
  11. Similarly with instance of AVCaptureVideoDataOutput add video settings using dictionary and set “setAlwaysDiscardsLateVideoFrames” to yes for this example otherwise as per need.
  12. create a serial dispatch queue.
  13. assign SampleBufferDelegate class and queue to AVCaptureVideoDataOutput instance.
  14. Check whether session can add output to AVCaptureVideoDataOutput instance or not, if yes
  15. add output to AVCaptureVideoDataOutput instance.
  16. setup connection with media type using videoDataOutput and then enable the connection.
  17. create instance of AVCaptureVideoPreviewLayer with the same session.
  18. set background colour and gravity to preview layer.
  19. get the layer from preview view and set its bound to PreviewLayer and then add Previewlayer as sublayer to root layer(preview view layer).
  20. start the session using startRunning method of session instance.

Setup done………

Now you have to take care of drawing rectangle for faces or facial parts using the delegate method

::: didOutputSampleBuffer  (Preferred one)

alternate method is 

::: didDropSampleBuffer

of protocol :: AVCaptureVideoDataOutputSampleBufferDelegate



2: Delegate Method Implementation
  1. create CVPixelBufferref from the sampleBuffer received in delegate method using CMSampleBufferGetImageBuffer method.
  2. create CFDictionaryRef of attachments from sample buffer with attachment mode propagate.
  3. create CIImage from pixel buffer instance using attachments dictionary.
  4. create imageOptions dictionary using current device orientation and use it for feature detection.
  5. use faceDetector featuresInImage method to detect features. it will return you array of features.
  6. get CMFormatDescriptionRef from SampleBuffer using CMSampleBufferGetFormatdescription method.
  7. get the rest of video being displayed on iOS device using CMVideoFormatDescriptionGetCleanAperture method.
  8. use the main queue and drawfaceboxes on features detected or features you want to highlight.

3: DrawFaceBoxesForFeatures implementation details
  1. get the sublayers from the previewLayer sublayers.
  2. get the sublayers count and features count.
  3. Start the drawing process using CATransaction begin method.
  4. setValue for CSTransactionDisableActions key of CATransaction. e.g.. [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
  5. from the sublayers get the layer labeled “FaceLayer” and hide it for all this type of layer.
  6. check whether any feature is detected or faceDetection is enabled by user and on the basis of decide whether to commit transaction now or not.
  7. Find where the video box is positioned within the preview layer based on the video size and gravity using PreviewView size previewLayer Gravity feature
  8. For each feature detected, draw a feature layer and add it to previewLayer and draw rect (i used image in this sample code, same can also be used or we can draw an square).
  9. Apply orientation as per device orientation.
  10. once done for all features detected then commit CATransaction.

4: Additional Feature:
  1.   You can provide feature like handling pinch gesture and detectFace user input to enable or disable face detection… remember to draw face rect each time user enable or disable.
  2. TakePicture with face detection highlighted feature or without face marker.

5: Take Picture and saving it to cameraRoll is explained below:

Utility method from Apple:
CreateCGImageFromCVPixelBuffer

Implementation::::
static OSStatus CreateCGImageFromCVPixelBuffer(CVPixelBufferRef pixelBuffer, CGImageRef *imageOut) 
{
OSStatus err = noErr;
OSType sourcePixelFormat;
size_t width, height, sourceRowBytes;
void *sourceBaseAddr = NULL;
CGBitmapInfo bitmapInfo;
CGColorSpaceRef colorspace = NULL;
CGDataProviderRef provider = NULL;
CGImageRef image = NULL;

sourcePixelFormat = CVPixelBufferGetPixelFormatType( pixelBuffer );
if ( kCVPixelFormatType_32ARGB == sourcePixelFormat )
bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaNoneSkipFirst;
else if ( kCVPixelFormatType_32BGRA == sourcePixelFormat )
bitmapInfo = kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst;
else
return -95014; // only uncompressed pixel formats

sourceRowBytes = CVPixelBufferGetBytesPerRow( pixelBuffer );
width = CVPixelBufferGetWidth( pixelBuffer );
height = CVPixelBufferGetHeight( pixelBuffer );

CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
sourceBaseAddr = CVPixelBufferGetBaseAddress( pixelBuffer );

colorspace = CGColorSpaceCreateDeviceRGB();
    
CVPixelBufferRetain( pixelBuffer );
provider = CGDataProviderCreateWithData( (void *)pixelBuffer, sourceBaseAddr, sourceRowBytes * height, ReleaseCVPixelBuffer);
image = CGImageCreate(width, height, 8, 32, sourceRowBytes, colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault);

bail:
if ( err && image ) {
CGImageRelease( image );
image = NULL;
}
if ( provider ) CGDataProviderRelease( provider );
if ( colorspace ) CGColorSpaceRelease( colorspace );
*imageOut = image;
return err;
}
:::
&
CreateCGBitmapContextForSize

Implementation:::
static CGContextRef CreateCGBitmapContextForSize(CGSize size)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow = (size.width * 4);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    context = CGBitmapContextCreate (NULL,
size.width,
size.height,
8,      // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGContextSetAllowsAntialiasing(context, NO);
    CGColorSpaceRelease( colorSpace );
    return context;
}
:::

Process to save image to camera roll:
  1. Find out the current orientation and tell the still image output.
  2. get stillImageConnection from stillImageOutput with mediaType as AVMediaTypeVideo.
  3. get current device orientation.
  4. get avorientation for current device orientation.
  5. Check out helper method below for avOrientation.
  6. using StillImageConnetion set video orientation as avOrientation.
  7. set video scale and crop factor in StillImageConnection.
  8. Check whether face detection is ON or not? on the basis of that: set the appropriate pixel format / image type output setting depending on if we'll need an uncompressed image for the possiblity of drawing the red square over top or if we're just writing a jpeg to the camera roll which is the trival case.
  9. on the basis of whether face detection is on or not…we have to set the output settings for StillImageOutput object as explained above.
  10. Now Call captureStillImageAsynchronouslyFromConnection Method and inside completion handler check for error, if error show proper error message to user else do faceDetection bool value check and perform save to camera roll operation.
  11. Considering face detection is on, then:
  12. create pixel buffer from imageDataSampleBuffer using CMSampleBufferGetImageBuffer method.
  13. get the attachment dictionary using CMCopyDictionaryOfAttachments method.
  14. create CIImage object using pixel buffer and attachment objects.
  15. get orientation from the imageDataSampleBuffer using CMGetAttachment method and create imageOptions dictionary with this orientation and CIDetectorImageOrientation key.
  16. now dispatch synchronously the videoDataOutputQueue, this will ensure that new frames are automatically dropped while we are processing the existing frames.
  17. in the dispatch_sync method, do as described for this kind of app demo…
  18. get the features in image as a featuresArray.
  19. create CGImageRef using the CreateCGImageFromCVPixelBuffer helper method as described above.
  20. As face detection was on and in that case we want image to saved with square on face, so using the CGImageRef create another CGImageRef with square overlayed using another helper method newSquareOverlayedImageForFeatures will be described later.
  21. Now write the CGImageRef to camera roll, remember we can write to camera roll using the ALAssetsLibrary but using this we can write standard compressed format images, thus to write CGImageRef we need to do some additional processing.
  22. Check out writeCGImageToCameraRoll helper method for this processing.


  1. Now suppose face detection was off then:
  2. using jpegStillImageNSDataRepresentation method of ACCaptureStillImageOutput get the jpegdata object.
  3. get the attachments dictionary ref from sample buffer data.
  4. create instance of ALAssetsLibrary and write image data to photo album using writeImageDataToSavedPhotosAlbum method.

Done…

Additional Helper Methods:

// utility routine to create a new image with the red square overlay with appropriate orientation
// and return the new composited image which can be saved to the camera roll

// used with take picture method only.
- (CGImageRef)newSquareOverlayedImageForFeatures:(NSArray *)features 
inCGImage:(CGImageRef)backgroundImage 
 withOrientation:(UIDeviceOrientation)orientation 
 frontFacing:(BOOL)isFrontFacing
{
CGImageRef returnImage = NULL;
CGRect backgroundImageRect = CGRectMake(0., 0., CGImageGetWidth(backgroundImage), CGImageGetHeight(backgroundImage));
CGContextRef bitmapContext = CreateCGBitmapContextForSize(backgroundImageRect.size);
CGContextClearRect(bitmapContext, backgroundImageRect);
CGContextDrawImage(bitmapContext, backgroundImageRect, backgroundImage);
CGFloat rotationDegrees = 0.;

switch (orientation) {
case UIDeviceOrientationPortrait:
rotationDegrees = -90.;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotationDegrees = 90.;
break;
case UIDeviceOrientationLandscapeLeft:
if (isFrontFacing) rotationDegrees = 180.;
else rotationDegrees = 0.;
break;
case UIDeviceOrientationLandscapeRight:
if (isFrontFacing) rotationDegrees = 0.;
else rotationDegrees = 180.;
break;
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
default:
break; // leave the layer in its last known orientation
}
UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];

    // features found by the face detector
for ( CIFaceFeature *ff in features ) {
CGRect faceRect = [ff bounds];
CGContextDrawImage(bitmapContext, faceRect, [rotatedSquareImage CGImage]);
}
returnImage = CGBitmapContextCreateImage(bitmapContext);
CGContextRelease (bitmapContext);

return returnImage;
}

// utility routine used after taking a still image to write the resulting image to the camera roll
- (BOOL)writeCGImageToCameraRoll:(CGImageRef)cgImage withMetadata:(NSDictionary *)metadata
{
CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(destinationData, 
CFSTR("public.jpeg"), 
1, 
NULL);
BOOL success = (destination != NULL);
require(success, bail);
const float JPEGCompQuality = 0.85f; // JPEGHigherQuality
CFMutableDictionaryRef optionsDict = NULL;
CFNumberRef qualityNum = NULL;

qualityNum = CFNumberCreate(0, kCFNumberFloatType, &JPEGCompQuality);    
if ( qualityNum ) {
optionsDict = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if ( optionsDict )
CFDictionarySetValue(optionsDict, kCGImageDestinationLossyCompressionQuality, qualityNum);
CFRelease( qualityNum );
}

CGImageDestinationAddImage( destination, cgImage, optionsDict );
success = CGImageDestinationFinalize( destination );
if ( optionsDict )
CFRelease(optionsDict);

require(success, bail);

CFRetain(destinationData);
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library writeImageDataToSavedPhotosAlbum:(id)destinationData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
if (destinationData)
CFRelease(destinationData);
}];
[library release];
bail:
if (destinationData)
CFRelease(destinationData);
if (destination)
CFRelease(destination);
return success;
}

// utility routine to display error aleart if takePicture fails
- (void)displayErrorOnMainQueue:(NSError *)error withMessage:(NSString *)message
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ (%d)", message, (int)[error code]]
message:[error localizedDescription]
  delegate:nil 
 cancelButtonTitle:@"Dismiss" 
 otherButtonTitles:nil];
[alertView show];
[alertView release];
});
}

Use this delegate method to perform some animation while capturing and saving image to Camera Roll:

// perform a flash bulb animation using KVO to monitor the value of the capturingStillImage property of the AVCaptureStillImageOutput class
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ( context == AVCaptureStillImageIsCapturingStillImageContext ) {
BOOL isCapturingStillImage = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];

if ( isCapturingStillImage ) {
// do flash bulb like animation
flashView = [[UIView alloc] initWithFrame:[previewView frame]];
[flashView setBackgroundColor:[UIColor whiteColor]];
[flashView setAlpha:0.f];
[[[self view] window] addSubview:flashView];

[UIView animateWithDuration:.4f
animations:^{
[flashView setAlpha:1.f];
}
];
}
else {
[UIView animateWithDuration:.4f
animations:^{
[flashView setAlpha:0.f];
}
completion:^(BOOL finished){
[flashView removeFromSuperview];
[flashView release];
flashView = nil;
}
];
}
}
}

Smile and Eye Blink Detection:
-(void)updateUIForFeatures:(NSString*)feature value:(BOOL)args
{
    if ([feature isEqualToString:@"smile"]) {
        self.smileImgView.hidden = NO;
        self.lEyeImgView.hidden = YES;
        self.REyeImgView.hidden = YES;
        self.txtLbl.hidden = NO;
        self.txtLbl.text = @"Smiling......";
    }
    if ([feature isEqualToString:@"leftEye"]) {
        self.lEyeImgView.hidden = NO;
        self.smileImgView.hidden = YES;
        self.REyeImgView.hidden = YES;
        self.txtLbl.hidden = NO;
        self.txtLbl.text = @"Left Eye Closed......";
    }
    
    if ([feature isEqualToString:@"rightEye"]) {
        self.REyeImgView.hidden = NO;
        self.smileImgView.hidden = YES;
        self.lEyeImgView.hidden = YES;
        self.txtLbl.hidden = NO;
        self.txtLbl.text = @"Right Eye Closed......";
    }
    
}
We will be using delegate method for the calling of this:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{ 
 // got an image
 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
 CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
 CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(NSDictionary *)attachments];
 if (attachments)
  CFRelease(attachments);
 NSDictionary *imageOptions = nil;
 UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
 int exifOrientation;
        
 enum {
  PHOTOS_EXIF_0ROW_TOP_0COL_LEFT   = 1, //   1  =  0th row is at the top, and 0th column is on the left (THE DEFAULT).
  PHOTOS_EXIF_0ROW_TOP_0COL_RIGHT   = 2, //   2  =  0th row is at the top, and 0th column is on the right.  
  PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT      = 3, //   3  =  0th row is at the bottom, and 0th column is on the right.  
  PHOTOS_EXIF_0ROW_BOTTOM_0COL_LEFT       = 4, //   4  =  0th row is at the bottom, and 0th column is on the left.  
  PHOTOS_EXIF_0ROW_LEFT_0COL_TOP          = 5, //   5  =  0th row is on the left, and 0th column is the top.  
  PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP         = 6, //   6  =  0th row is on the right, and 0th column is the top.  
  PHOTOS_EXIF_0ROW_RIGHT_0COL_BOTTOM      = 7, //   7  =  0th row is on the right, and 0th column is the bottom.  
  PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM       = 8  //   8  =  0th row is on the left, and 0th column is the bottom.  
 };
 
 switch (curDeviceOrientation) {
  case UIDeviceOrientationPortraitUpsideDown:  // Device oriented vertically, home button on the top
   exifOrientation = PHOTOS_EXIF_0ROW_LEFT_0COL_BOTTOM;
   break;
  case UIDeviceOrientationLandscapeLeft:       // Device oriented horizontally, home button on the right
   if (isUsingFrontFacingCamera)
    exifOrientation = PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT;
   else
    exifOrientation = PHOTOS_EXIF_0ROW_TOP_0COL_LEFT;
   break;
  case UIDeviceOrientationLandscapeRight:      // Device oriented horizontally, home button on the left
   if (isUsingFrontFacingCamera)
    exifOrientation = PHOTOS_EXIF_0ROW_TOP_0COL_LEFT;
   else
    exifOrientation = PHOTOS_EXIF_0ROW_BOTTOM_0COL_RIGHT;
   break;
  case UIDeviceOrientationPortrait:            // Device oriented vertically, home button on the bottom
  default:
   exifOrientation = PHOTOS_EXIF_0ROW_RIGHT_0COL_TOP;
   break;
 }

 imageOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:exifOrientation] forKey:CIDetectorImageOrientation];
 NSArray *features = [faceDetector featuresInImage:ciImage options:@{CIDetectorEyeBlink: @YES,
                                                                        CIDetectorSmile: @YES,
                                                                        CIDetectorImageOrientation: [NSNumber numberWithInt:exifOrientation]}];
 [ciImage release];
    
    detectFacesFeatures = YES;
    for (CIFaceFeature *ff in features)
    {
        
        if (ff.hasSmile) {
            NSLog(@"has smile %d", 1);   
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                [self updateUIForFeatures:@"smile" value:ff.hasSmile];
            });
        }
        if (ff.leftEyeClosed) {
            NSLog(@"leftEyeClosed %d", 1);
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                [self updateUIForFeatures:@"leftEye" value:ff.leftEyeClosed];
            });
            
        }
        if (ff.rightEyeClosed) {
            NSLog(@"rightEyeClosed %d", 1);
            dispatch_async(dispatch_get_main_queue(), ^(void) {
                [self updateUIForFeatures:@"rightEye" value:ff.rightEyeClosed];
            });
        }
        
        if (ff.hasTrackingFrameCount) {
            NSLog(@"trackingFrameCount %d", ff.trackingFrameCount);
            
        }
        
        if (ff.hasTrackingID) {
            NSLog(@"trackingFrameCount %d", ff.trackingID);
            
        }
        
        if (ff.hasTrackingID) {
            NSLog(@"trackingFrameCount %d", ff.trackingID);
            
        }
        
        NSLog(@"type %@", ff.type);
       // NSLog(@"face bounds %@", NSStringFromCGRect(faceRect));
        
        if (ff.hasFaceAngle){
            NSLog(@"faceAngle %g", ff.faceAngle);
        }
        
        if (ff.hasMouthPosition){
            NSLog(@"Mouth %g %g", ff.mouthPosition.x, ff.mouthPosition.y);
        }
        
        if (ff.hasRightEyePosition){
            NSLog(@"right eye %g %g", ff.rightEyePosition.x, ff.rightEyePosition.y);
        }
        
        if (ff.hasLeftEyePosition){
            NSLog(@"right eye %g %g", ff.leftEyePosition.x, ff.leftEyePosition.y);
        }
        
    }

 CMFormatDescriptionRef fdesc = CMSampleBufferGetFormatDescription(sampleBuffer);
 CGRect clap = CMVideoFormatDescriptionGetCleanAperture(fdesc, false /*originIsTopLeft == false*/);
 
 dispatch_async(dispatch_get_main_queue(), ^(void) {
  [self drawFaceBoxesForFeatures:features forVideoBox:clap orientation:curDeviceOrientation];
 });
}
Thank you for reading this blog. Post your feed back and queries.