Video avatar fixes

This commit is contained in:
Ilya Laktyushin 2020-07-02 16:36:51 +03:00
parent fb5c983f04
commit b157e92eb2
5 changed files with 66 additions and 3 deletions

View File

@ -23,8 +23,12 @@
#import <LegacyComponents/TGMenuView.h> #import <LegacyComponents/TGMenuView.h>
#import "TGPaintFaceDetector.h"
@interface TGMediaPickerGalleryPhotoItemView () @interface TGMediaPickerGalleryPhotoItemView ()
{ {
SMetaDisposable *_facesDisposable;
UILabel *_fileInfoLabel; UILabel *_fileInfoLabel;
TGMessageImageViewOverlayView *_progressView; TGMessageImageViewOverlayView *_progressView;
@ -57,6 +61,8 @@
self = [super initWithFrame:frame]; self = [super initWithFrame:frame];
if (self != nil) if (self != nil)
{ {
_facesDisposable = [[SMetaDisposable alloc] init];
__weak TGMediaPickerGalleryPhotoItemView *weakSelf = self; __weak TGMediaPickerGalleryPhotoItemView *weakSelf = self;
_imageView = [[TGModernGalleryImageItemImageView alloc] init]; _imageView = [[TGModernGalleryImageItemImageView alloc] init];
_imageView.clipsToBounds = true; _imageView.clipsToBounds = true;
@ -101,6 +107,7 @@
{ {
[_adjustmentsDisposable dispose]; [_adjustmentsDisposable dispose];
[_attributesDisposable dispose]; [_attributesDisposable dispose];
[_facesDisposable dispose];
} }
- (void)setHiddenAsBeingEdited:(bool)hidden - (void)setHiddenAsBeingEdited:(bool)hidden
@ -226,8 +233,11 @@
}]]; }]];
if (!item.asFile) if (!item.asFile) {
[_facesDisposable setDisposable:[[TGPaintFaceDetector detectFacesInItem:item.editableMediaItem editingContext:item.editingContext] startWithNext:nil]];
return; return;
}
_fileInfoLabel.text = nil; _fileInfoLabel.text = nil;

View File

@ -41,6 +41,7 @@
#import <LegacyComponents/TGMenuView.h> #import <LegacyComponents/TGMenuView.h>
#import "PGPhotoEditor.h" #import "PGPhotoEditor.h"
#import "TGPaintFaceDetector.h"
@interface TGMediaPickerGalleryVideoItemView() <TGMediaPickerGalleryVideoScrubberDataSource, TGMediaPickerGalleryVideoScrubberDelegate> @interface TGMediaPickerGalleryVideoItemView() <TGMediaPickerGalleryVideoScrubberDataSource, TGMediaPickerGalleryVideoScrubberDelegate>
{ {
@ -96,6 +97,7 @@
SMetaDisposable *_adjustmentsDisposable; SMetaDisposable *_adjustmentsDisposable;
SMetaDisposable *_attributesDisposable; SMetaDisposable *_attributesDisposable;
SMetaDisposable *_downloadDisposable; SMetaDisposable *_downloadDisposable;
SMetaDisposable *_facesDisposable;
SMetaDisposable *_currentAudioSession; SMetaDisposable *_currentAudioSession;
SVariable *_editableItemVariable; SVariable *_editableItemVariable;
@ -131,6 +133,7 @@
_currentAudioSession = [[SMetaDisposable alloc] init]; _currentAudioSession = [[SMetaDisposable alloc] init];
_playerItemDisposable = [[SMetaDisposable alloc] init]; _playerItemDisposable = [[SMetaDisposable alloc] init];
_facesDisposable = [[SMetaDisposable alloc] init];
_videoDurationVar = [[SVariable alloc] init]; _videoDurationVar = [[SVariable alloc] init];
_videoDurationDisposable = [[SMetaDisposable alloc] init]; _videoDurationDisposable = [[SMetaDisposable alloc] init];
@ -251,6 +254,7 @@
[_thumbnailsDisposable dispose]; [_thumbnailsDisposable dispose];
[_attributesDisposable dispose]; [_attributesDisposable dispose];
[_downloadDisposable dispose]; [_downloadDisposable dispose];
[_facesDisposable dispose];
[self stopPlayer]; [self stopPlayer];
[self releaseVolumeOverlay]; [self releaseVolumeOverlay];
@ -385,8 +389,13 @@
[super setItem:item synchronously:synchronously]; [super setItem:item synchronously:synchronously];
if (itemChanged) if (itemChanged) {
[self _playerCleanup]; [self _playerCleanup];
if (!item.asFile) {
[_facesDisposable setDisposable:[[TGPaintFaceDetector detectFacesInItem:item.editableMediaItem editingContext:item.editingContext] startWithNext:nil]];
}
}
_scrubberView.allowsTrimming = false; _scrubberView.allowsTrimming = false;
_videoDimensions = item.dimensions; _videoDimensions = item.dimensions;

View File

@ -1,6 +1,9 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <SSignalKit/SSignalKit.h> #import <SSignalKit/SSignalKit.h>
@protocol TGMediaEditableItem;
@class TGMediaEditingContext;
@interface TGPaintFaceFeature : NSObject @interface TGPaintFaceFeature : NSObject
{ {
CGPoint _position; CGPoint _position;
@ -49,6 +52,8 @@
+ (SSignal *)detectFacesInImage:(UIImage *)image originalSize:(CGSize)originalSize; + (SSignal *)detectFacesInImage:(UIImage *)image originalSize:(CGSize)originalSize;
+ (SSignal *)detectFacesInItem:(id<TGMediaEditableItem>)item editingContext:(TGMediaEditingContext *)editingContext;
@end @end

View File

@ -3,6 +3,8 @@
#import <LegacyComponents/TGPaintUtils.h> #import <LegacyComponents/TGPaintUtils.h>
#import <ImageIO/ImageIO.h> #import <ImageIO/ImageIO.h>
#import "TGMediaEditingContext.m"
@interface TGPaintFace () @interface TGPaintFace ()
+ (instancetype)faceWithBounds:(CGRect)bounds angle:(CGFloat)angle leftEye:(TGPaintFaceEye *)leftEye rightEye:(TGPaintFaceEye *)rightEye mouth:(TGPaintFaceMouth *)mouth; + (instancetype)faceWithBounds:(CGRect)bounds angle:(CGFloat)angle leftEye:(TGPaintFaceEye *)leftEye rightEye:(TGPaintFaceEye *)rightEye mouth:(TGPaintFaceMouth *)mouth;
@ -26,6 +28,41 @@
@implementation TGPaintFaceDetector @implementation TGPaintFaceDetector
+ (SSignal *)detectFacesInItem:(id<TGMediaEditableItem>)item editingContext:(TGMediaEditingContext *)editingContext
{
CGSize originalSize = item.originalSize;
SSignal *cachedFaces = [editingContext facesForItem:item];
SSignal *cachedSignal = [cachedFaces mapToSignal:^SSignal *(id result)
{
if (result == nil)
return [SSignal fail:nil];
return [SSignal single:result];
}];
SSignal *imageSignal = [item screenImageSignal:0];
SSignal *detectSignal = [[[imageSignal filter:^bool(UIImage *image)
{
if (![image isKindOfClass:[UIImage class]])
return false;
if (image.degraded)
return false;
return true;
}] take:1] mapToSignal:^SSignal *(UIImage *image) {
return [[TGPaintFaceDetector detectFacesInImage:image originalSize:originalSize] startOn:[SQueue concurrentDefaultQueue]];
}];
return [[[cachedSignal catch:^SSignal *(__unused id error)
{
return detectSignal;
}] deliverOn:[SQueue mainQueue]] onNext:^(NSArray *next)
{
[editingContext setFaces:next forItem:item];
}];
}
+ (SSignal *)detectFacesInImage:(UIImage *)image originalSize:(CGSize)originalSize + (SSignal *)detectFacesInImage:(UIImage *)image originalSize:(CGSize)originalSize
{ {
return [[SSignal alloc] initWithGenerator:^id<SDisposable>(SSubscriber *subscriber) return [[SSignal alloc] initWithGenerator:^id<SDisposable>(SSubscriber *subscriber)

View File

@ -263,7 +263,9 @@ const CGFloat TGPhotoAvatarPreviewLandscapePanelSize = TGPhotoAvatarPreviewPanel
- (void)prepareTransitionInWithReferenceView:(UIView *)referenceView referenceFrame:(CGRect)referenceFrame parentView:(UIView *)parentView noTransitionView:(bool)noTransitionView - (void)prepareTransitionInWithReferenceView:(UIView *)referenceView referenceFrame:(CGRect)referenceFrame parentView:(UIView *)parentView noTransitionView:(bool)noTransitionView
{ {
[super prepareTransitionInWithReferenceView:referenceView referenceFrame:referenceFrame parentView:parentView noTransitionView:noTransitionView]; [super prepareTransitionInWithReferenceView:referenceView referenceFrame:referenceFrame parentView:parentView noTransitionView:noTransitionView];
[self.view insertSubview:_transitionView belowSubview:_wrapperView];
if (self.initialAppearance && self.fromCamera)
[self.view insertSubview:_transitionView belowSubview:_wrapperView];
} }
- (void)transitionIn - (void)transitionIn