AVCaptureSessionPreset Photo and High Optimization - ios

I have been trying all kinds of settings for the AVCaptureSessionPreset to match my desired output, but I don't seem to be able to get it right.
The Photo preset captures a photo where the resolution is too high, so it takes some time before the image is finished processing.
The High preset is perfect in the sense of performance. The image gets processed and returned almost instantaneously. But the aspect ratio is not right, it is 16:9 compared to the Photo preset which is 4:3.
I have also tried changing the AVCaptureDevice's activeFormat to a lower resolution. But the performance is just not as good as when using the High preset.
Someone with a similar problem from 2014:
AVCaptureSession preset creates a photo that is too big

The problem seems to be that you are attempting to perform some kind of time-consuming processing on the large photo data returned from the capture. Don't. It's large! Instead, when you configure the session, ask for a preview image at the desired size, and when the capture takes place, obtain the preview image and operate on that.

Related

How to configure AVCaptureSession for high res still images and low res (video) preview?

I'd like to capture high resolution still images using AVCaptureSession. Therefore AVCaptureSession preset is set to Photo.
This is working well so far. On an iPhone 4 the final still image resolution is at its maximum of 2448x3264 pixels and the preview (video) resolution is 852x640 pixels.
Now, because the preview frames are analyzed to detect objects in the scene, I'd like to lower their resolution. How can this be done?
I've tried to set AVVideoSettings with a lower width/height to AVCaptureVideoDataOutput, but this leads to the following error message:
AVCaptureVideoDataOutput setVideoSettings:] - videoSettings dictionary contains one or more unsupported (ignored) keys: (AVVideoHeightKey, AVVideoWidthKey
So it seems this is not the right approach to configure the size of the preview frames received by AVCaptureVideoDataOutput / AVCaptureVideoDataOutputSampleBufferDelegate. Do you have any ideas how the resolution of the preview frames can be configured?
Any advise is welcome,
Thank you.
If you want to specify the settings manually, you need to set activeFormat on the AVCaptureDevice. This will be implicitly set the session preset to AVCaptureSessionPresetInputPriority.
The activeFormat takes a AVCaptureDeviceFormat but you can only take one from the list of AVCaptureDevice.formats. You'll need to go through the list and find one that fits your needs. Specifically, check that highResolutionStillImageDimensions is high enough for desired still capture and formatDescription (which needs to be inspected with CMFormatDescription* functions, e.g., CMVideoFormatDescriptionGetDimensions) matches your desired preview settings.
Just for the records: I ended up configuring AVCaptureSession in preset Low while aiming the camera. As soon as the shutter is triggered, the app switches to preset Photo, performs a focus run and takes the picture. This way it takes between 1 and 2.5 seconds to take a picture, which isn't that great, but it's at least a workaround.
To lower the size of the output of AVCaptureVideoDataOutput you can set the bitrate to be lower thus producing a small sample size.
commonly used keys for AVCaptureVideoDataOutput are:
AVVideoAverageBitRateKey
AVVideoProfileLevelKey
AVVideoExpectedSourceFrameRateKey
AVVideoMaxKeyFrameIntervalKey
For example:
private static let videoCompressionOptionsMedium = [AVVideoAverageBitRateKey : 1750000,
AVVideoProfileLevelKey : AVVideoProfileLevelH264BaselineAutoLevel,
AVVideoExpectedSourceFrameRateKey : Int(30),
AVVideoMaxKeyFrameIntervalKey : Int(30)]

Processing live video and still images simultaneously at two different resolutions on iPhone?

I'm working on video processing app for the iPhone using OpenCV.
For performance reasons, I wan't to process live video at a relatively low resolution. I'm doing object-detection on each frame in the video. When the objects are found in the low-resolution video frame, I need to acquire that exact same frame at a much higher resolution.
I've been able to semi-accomplish this using a videoDataBufferOutput and a stillImageOutput from AVFoundation, but the still image is not the exact frame that I need.
Are there any good implementations of this or ideas on how to implement it myself?
In AVCaptureSessionPresetPhoto it use small video preview(about 1000x700 for iPhone6) and high resolution photo(about 3000x2000).
So I use modified 'CvPhotoCamera' class to process small preview and take photo of full-size picture. I post this code here: https://stackoverflow.com/a/31478505/1994445

How can I make AVCaptureSessionpresetPhoto take pictures at a lower resolution?

I'm currently using AVCaptureSessionpresetPhoto to take my pictures and I'm adding filters to them. Problem is that the resolution is so big that I have memory warnings ringing all over the place. The picture is simply way to large to process. It crashes my app every single time. Is there anyway I can specify the resolution to shoot at?
EDIT**
Photography apps like Instagram or the Facebook Camera app for example can do this without any problems. These applications can take pictures at high resolutions, scale them down and process them without any delay. I did a comparison check, the native iOS camera maintains a much higher quality resolution when compared to pictures taken by other applications. The extreme level of quality isn't really needed required for a mobile platform so it seems as if these images are being taken at lower resolution to allow for faster processing and quick upload times. Thus there must be a way to shoot at a lower resolution. If anyone has a solution to this problem, it would greatly be appreciated!
You need to re-size image after capture image using AVCaptureSession and store it's image after resizing.
You found lots of similar question in to StackOverlow i just putting some link bellow that makes help's you.
One More thing As my suggestion that using SDWebImage for Displaying Images asynchronously Becouse App working smoothly. There are also some other way for example(Grand Central Dispatch (GCD) Reference , NSOperationQueue etc) in iOS for asynchronous Tast
Re-size Image:-
How to resize an image in iOS?
UIImage resizing not working properly
How to ReSize Image with Good Quality in iPhone
How to resize the image programmatically in objective-c in iphone

Capturing image at higher resolution in iOS

Currently i am working on an image app where i am able to capture an image by using AVFoundation framework. But what i am looking is , to capture an image with certain resolution and DPI (may be 300 DPI or greater).
How to do this ?
There have been numerous posts on here about trying to do OCR on camera generated images. The problem is not that the resolution is too low, but that its too high. I cannot find a link right now, but there was a question a year or so ago where in the end, if the image size was reduced by a factor of four or so, the OCR engine worked better. If you examine the image in Preview, what you want is the number of pixels per character to be say 16x16 or 32x32, not 256x256. Frankly I don't know the exact number but I'm sure you can research this and find posts from actual framework users telling you the best size.
Here is a nice response on how to best scale a large image (with a link to code).

Tesseract OCR Camera

I'm using Tesseract OCR 3.01 in my iOS application, it shows 90% accuracy for my data when I pick an image from my phone’s library. But if I use the same image from the camera, it is showing jumbled letters. I followed this tutorial, kindly guide me if something can be done to make sure it works from camera as it works for gallery images.
Yup, There are three things to be specific, First of all, OCR works well with black and white images rather than colored, So If you could try to convert your image to B&W, it would increase accuracy.
The second thing is the size and orientation, You need to force the image to be of 640*480 or 320 size, this would increase both the speed of recognition and the accuracy as well, For orientation , there are a lot of ways to manage.
Finally, If some how you can allow the user to specify Exactly where or on which part of the image he wants to perform the OCR, this greatly increases the accuracy and time since the library does not need to check the entire image for text, rather you already specify the part to be searched for.
PS:I have been working on creating an OCR app for the past few weeks.
Almost for sure the problem is "orientation". Apple tends to create images in one bit map form - the image bits are laid out as if the camera was on its side with the volume buttons top and right. Images that you see which appear taller than wider are still laid out as above, but there is an "orientation" in the EXIF object included with the image.
I'm going to guess that tesseract does not look at the EXIF, but expects the image in a "standard" format so that text is in the position it would be for a person reading the text.
You can test my hypothesis by using camera images taken with volume button top right.
If they work, then what you will need to do is process the image yourself, and re-arrange the bits per the orientation setting. This is not all that hard to do but will require you to read up on vImage and/or bit map contexts.

Resources