What is the best approach for writing a program to identify objects in a picture then crop them a specific way? My works quality control department is responsible for taking pictures of our products at various phases through our QC process and currently the process goes:


Take picture of product
Crop the picture down to only the product
Name the cropped picture to whatever the part is and some other relevant data


Depending on the type of product the pictures will be cropped a certain way. So my initial thought would be to use a reference to an object identifier and then once the object is identified it will use a cropping method specific to that product. There will also be QR codes within the pictures being taken for naming via OCR in the future so I can probably identify the parts that way if this proves slow or problematic.

The part I am unsure about is how to get the program to know how to crop based on a part. For example I would like to present the program with a couple before crop and after crop photos of product X then make a specific cropping formula for product X based on those two inputs. 

Also if it makes any difference my code is in C#
Depending on kind and amount of data you posess, there are few approaches that you might consider.


Marking target objects on dataset and training CNN that returns coordinates of target object. In this case, remember that it is usually faster when training data ROIs have their coordinates relative to image size.
Use some kind of focus mechanism, like spatial transformer network:


https://arxiv.org/abs/1506.02025


This kind of network component is able to learn image transformation (including crop) that maximazes target metric for main classifier. This tutorial on pytorch:


https://pytorch.org/tutorials/intermediate/spatial_transformer_tutorial.html


shows some nice visualizations of STN results. Good thing about this kind of network is that, given enough data, it might learn proper transformation from image classification data (photo -> class). One does not need to explicitly mark target objects on image!
Object detection networks, like YOLO, Faster-RCNN. There are many tutorials on that matter, eg:


https://www.datacamp.com/community/tutorials/object-detection-guide

Saliency extraction. Simple idea is to generate heatmap showing what parts of input image activates classifier the most. I guess you could try calculate bounding box basing on such heatmap. Example research paper:


https://arxiv.org/abs/1805.08249



Points 1 and 2 are probably easies to implement, so I would start with them.