Find objects visible to camera

I’m wondering if it is possible to get the click selection behaviour in OpenMaya.MGlobal.selectFromScreen method. I found this code snippet online

import maya.OpenMaya as om
import maya.OpenMayaUI as omUI
view = omUI.M3dView.active3dView()
om.MGlobal.selectFromScreen( 0, 0, view.portWidth(), view.portHeight(),om.MGlobal.kReplaceList)

It selects everything, even objects behind other objects. So even though the object is in the viewport, you cannot actually see it. Like if I put a sphere at origin and put a plane in between sphere and camera so you can’t see it. Is this possible with selectFromScreen? Or has someone already solved this problem somewhere else?